omz:forum

    • Register
    • Login
    • Search
    • Recent
    • Popular

    Welcome!

    This is the community forum for my apps Pythonista and Editorial.

    For individual support questions, you can also send an email. If you have a very short question or just want to say hello — I'm @olemoritz on Twitter.


    Change a ui.NavigationView bar color

    Pythonista
    4
    17
    7210
    Loading More Posts
    • Oldest to Newest
    • Newest to Oldest
    • Most Votes
    Reply
    • Reply as topic
    Log in to reply
    This topic has been deleted. Only users with topic management privileges can see it.
    • Phuket2
      Phuket2 @robertiii last edited by

      @robertiii , I took the method that @technoway wrote in post and just changed it to a function. So you should be able to call that function with an already presented uiView and a hex color. Look, I dont really have any exp using the ui.Navigation view. It’s a little funky. So the below may not work, but its a starting point. I think it will work.
      Ok, hope it helps

      import ui
      from objc_util import ObjCInstance, UIColor
      from time import sleep
      
      
      def change_title_bar_color(v, hex_color):
          """ Change the title bar color to the passed color.
          """
          vv = ObjCInstance(v)
          bar_bckgnd = vv.superview().superview().superview().subviews()[1].subviews()[0]
          bar_bckgnd.backgroundColor = UIColor.colorWithHexString_(hex_color)
      
      
      class MyClass(ui.View):
          def __init__(self, *args, **kwargs):
              super().__init__(*args, **kwargs)
              self.make_view()
      
          def make_view(self):
              pass
      
      if __name__ == '__main__':
          f = (0, 0, 300, 400)
          v = MyClass(frame=f)
          v.present(style='sheet', animated=False)
          '''
          You may or may not need the sleep method as I have done below. If I dont
          use it on my iPad pro, I get an error, something about obj does not have
          superview.  Basically going to fast. The function is being called before
          the view is fully created.
          if you didnt want the sleep, quite a few different ways you could work
          around it.
          Maybe one of the guys could comment if using @on_main_thread decorator on
          the function would stop this from happening or not.
          I could experiment, but I would not understand what is really happening.
          '''
          sleep(.01)
          change_title_bar_color(v, '#ff0000')
      
      1 Reply Last reply Reply Quote 0
      • Phuket2
        Phuket2 last edited by

        Sorry ignore all my comments about sleep. Just remove it. The problem comes from me using animated = False, when the view is presented.bJust dont use that param and it's fine.

        1 Reply Last reply Reply Quote 0
        • robertiii
          robertiii last edited by

          Thank you so much!!!!!!!

          1 Reply Last reply Reply Quote 0
          • robertiii
            robertiii last edited by

            So I tried it and got an error... Noneobject has type superview.

            1 Reply Last reply Reply Quote 0
            • robertiii
              robertiii last edited by

              I have figured out its because I placed it before the present. But if i place it after it doesn’t change

              1 Reply Last reply Reply Quote 0
              • technoway
                technoway last edited by

                Hi. The method I wrote, that Phuket2 mentioned, was to change the title bar color, it probably won't work for your Navigation bar without modification.

                I just put some code that JonB wrote into a method - he really did the heavy lifting.

                1 Reply Last reply Reply Quote 0
                • JonB
                  JonB last edited by

                  If your navigationview is n:

                  ObjCInstance(n).navigationController().navigationBar().backgroundColor=UIColor.redColor()
                  

                  etc

                  1 Reply Last reply Reply Quote 1
                  • robertiii
                    robertiii last edited by

                    For some reason this fails to change the color, but no errors are given.

                    1 Reply Last reply Reply Quote 0
                    • JonB
                      JonB last edited by

                      @JonB said:

                      ObjCInstance(n).navigationController().navigationBar().backgroundColor=UIColor.redColor()

                      sorry, i meant

                      ObjCInstance(nav).navigationController().navigationBar()._backgroundView().backgroundColor=UIColor.redColor()
                      
                      1 Reply Last reply Reply Quote 0
                      • robertiii
                        robertiii last edited by

                        Still nothing changes. I’m not sure what I’m doing.

                        1 Reply Last reply Reply Quote 0
                        • JonB
                          JonB last edited by JonB

                          import ui
                          from objc_util import *
                          v=ui.View(frame=(0,0,576,576),bg_color='blue',name='view')
                          nav = ui.NavigationView(v,name='nav view')
                          v.left_button_items = [ui.ButtonItem(image=ui.Image.named('ios7_film_256'))]
                          ObjCInstance(nav).navigationController().navigationBar()._backgroundView().backgroundColor=UIColor.redColor()
                          nav.present('landscape')
                          

                          does this give you a white "nav view" bar on top, with a red "view" bar, followed by a blue main view?
                          if not, try

                          import ui
                          from objc_util import *
                          v=ui.View(frame=(0,0,576,576),bg_color='blue',name='view')
                          nav = ui.NavigationView(v,name='nav view')
                          v.left_button_items = [ui.ButtonItem(image=ui.Image.named('ios7_film_256'))]
                          def changecolor():
                                ObjCInstance(nav).navigationController().navigationBar()._backgroundView().backgroundColor=UIColor.redColor()
                          nav.present('landscape') 
                          ui.delay(changecolor, 2)
                          1 Reply Last reply Reply Quote 0
                          • robertiii
                            robertiii last edited by

                            The second works. So apparently there is no way to change it before presenting?

                            Phuket2 1 Reply Last reply Reply Quote 0
                            • Phuket2
                              Phuket2 @robertiii last edited by

                              @robertiii, to me appears you have to present it first. I just did some tests, and it appears to me it's impossible just using Pythonista ui methods to create an invisible view. Not sure why @omz choose not to have a param in the present method to allow the view to be hidden on presentation. Could be some technical issue. I did try setting the views hidden attr to True be presenting. It does not work. But interestingly, if you set the view's bg_color to say 'blue' and have the views hidden attr set to True, you dont see the bg_color when the views presented.
                              I also tried the same with a second view after the first view had be created and presented on the screen. Same results. I tried this just in case it made a difference if there was at least one view presented on the screen or not. Maybe someone has more insight to this,

                              1 Reply Last reply Reply Quote 0
                              • JonB
                                JonB last edited by

                                you might try wrapping changecolor with a @on_main_thread. I am pretty sure i was able to change color prior to present, but maybe present is changing some defaults.

                                @phuket
                                present does not automatically set hidden=False (panel does, sheet does not). not being presented (no parent) and being hidden are two completely separate things.

                                1 Reply Last reply Reply Quote 0
                                • First post
                                  Last post
                                Powered by NodeBB Forums | Contributors