How to add custom navbar to full-screen VC in iOS 7 & tint status bar to match?

Discussion in 'iOS Programming' started by Duncan C, Dec 19, 2013.

  1. Duncan C macrumors 6502a

    Duncan C

    Joined:
    Jan 21, 2008
    Location:
    Northern Virginia
    #1
    In an app I'm developing (Xcode 5, >=iOS 7 auto-layout), I push a modal view controller. I want the modal view controller to have a navigation bar, so I add one, and add a constraint that positions it with it's top aligned with the top layout guide, so it is placed just under the status bar.
    (I'm using my own navigation bar because I want to take advantage of the system's management of the edit button and add button nav bar items. )
    The standard navigation bar has a very subtle light gray tint to it.
    With navigation controllers, the system somehow tints the status bar, or extends the navigation bar up so that it colors the status bar the same color as the navigation bar, and the effect is as if the status bar is part of a taller navigation bar.
    I don't know how to get this effect with a navigation bar I add to a window that is not managed by a navigation controller. I have vague memories of there being some sort of property I need to set that tells the status bar to adopt the tint color of the navigation bar, but despite searching and searching, I can't find it. I could put a view under the status bar and fiddle with it until it's color matches that of the navigation bar, but that's a hack, and if in the future the look of standard navigation bars changes, this approach would stop looking right.
    Does anybody know the secret of this?
     
  2. Duncan C thread starter macrumors 6502a

    Duncan C

    Joined:
    Jan 21, 2008
    Location:
    Northern Virginia
    #3
    Thanks for the reply but I still don't get it. My app does not have a custom tint color, and I don't want a custom tint color.

    I am adding a stock navigation bar to a modal window. I want the status bar to assume the color of the navigation bar, just like navigation bars that are managed by a navigation controller do.
     
  3. Ubuntu macrumors 68000

    Ubuntu

    Joined:
    Jul 3, 2005
    Location:
    UK/US
    #4
    What happens if you remove the top layout guide reference? My understanding is that the status bar is transparent, so naturally the view controller's content will start below it unless you use toplayoutguide or modify the VC's edgesForExtendedLayout property.

    Hope this helps.
     
  4. Duncan C thread starter macrumors 6502a

    Duncan C

    Joined:
    Jan 21, 2008
    Location:
    Northern Virginia
    #5
    That doesn't work either. If you do that the navigation bar gets placed under the transparent status bar, and they overlap with each other.

    What I want is the way a navigation bar draws in a navigation controller, where the status bar takes on the color of the navbar.
     
  5. Duncan C thread starter macrumors 6502a

    Duncan C

    Joined:
    Jan 21, 2008
    Location:
    Northern Virginia
    #6
    Ok, I just got the answer from Stack Overflow (link)

    You have to set up your view controller to conform to the UINavigationBarDelegate protocol:

    Code:
    @interface MyClass : UIViewController <UINavigationBarDelegate>
    
    Then make your view controller the navigation bar's delegate (can be done by control-dragging from the navigation bar to "files' owner" in IB)

    Finally, you need to add this method:

    Code:
    - (UIBarPosition)positionForBar:(id <UIBarPositioning>)bar 
    {
        return UIBarPositionTopAttached;
    }
    

     
  6. Ubuntu macrumors 68000

    Ubuntu

    Joined:
    Jul 3, 2005
    Location:
    UK/US
    #7
    Ah, I see. Sorry my post wasn't useful, and thanks for updating us with the answer.
     

Share This Page