Make Navigation Bar stretch behind status bar in xCode 5 / iOS7

11,581

You do want to set the barPosition of the UINavigationBar.

You can do this in code:

Let your ViewController conform to protocol UINavigationBarDelegate and implement positionBar: metod. (The protocol that you really need is UIBarPositioningDelegate but UINavigationBarDelegate does extend it.)

@interface SampleViewController () <UINavigationBarDelegate>
@property (weak, nonatomic) IBOutlet UINavigationBar *navigationBar;
@end

@implementation SampleViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    _navigationBar.delegate = self;
}

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

OR in Storyboard:

In the Identity Inspector of UINavigationBar, add a User Defined runtime Attribute with KeyPath = barPosition, Type = Number, and Value = 3:

Add User Defined Runtime Attribute

Share:
11,581
Omar
Author by

Omar

Updated on June 08, 2022

Comments

  • Omar
    Omar almost 2 years

    I have followed the following tutorial to move my navigation bar down so it is not covered by the status bar in xcode 5/ios7:

    Status bar and navigation bar issue in IOS7

    But now in iOS7 there is a blank space at the top where the status bar would be and I would like the navigation bar to fill this area too

    For instance, Facebook/twittter/Instagram iOS7 apps have the navigation bar background behind the status bar too. How do I achieve this?

    Sorry if I'm not being clear but really eager to get this sorted

    Thank you!

  • weienw
    weienw over 10 years
    You might want to explain how to actually set the barPosition.
  • lol
    lol over 9 years
    THANK YOU SO SO MUCH FOR TEACHING ME WHAT THE USER DEFINED RUNTIME ATTRIBUTES MENU DOES. THIS IS EARTH SHATTERING FOR ME. I will never subclass a ui element to change one property again.
  • Torsten B
    Torsten B about 9 years
    interesting find here, but it is not working for me. I always get the following error: "*** Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: 'Cannot manually set the delegate on a UINavigationBar managed by a controller.'" If i override UINavigationViewController and add it there, i do not get an error, but it is not working either...
  • TheCodingArt
    TheCodingArt over 8 years
    @lol I'm just hoping you're being sarcastic, O.O this is not something you should get in the habit of at all. It can quickly become a debugging nightmare.
  • lol
    lol over 8 years
    @TheCodingArt yeah I was reading some stuff on Quora by the Uber engineers who were saying that they delete all graphical UI design tools from Xcode -- because of exactly this; and the fact that the way the files are stored and parsed by Xcode cause a nightmare with version control. The comment definitely does have 4 up votes, but I think these should properly be regarded as for 'exigent circumstances' such as single author/mockup/non-production apps!
  • Christopher Swasey
    Christopher Swasey over 7 years
    It's funny how many engineers hate on storyboard and complain that they're awful for testing and debugging... and then you open up their code and they have reams and reams of glue code and boilerplate that is absolutely unparseable