7

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!

Community
  • 1
  • 1
Omar
  • 979
  • 3
  • 16
  • 26

2 Answers2

25

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

Sumner Evans
  • 8,951
  • 5
  • 30
  • 47
Carien van Zyl
  • 2,853
  • 22
  • 30
  • 10
    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. – lol Feb 19 '15 at 07:43
  • 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... – Torsten B Apr 24 '15 at 09:12
  • @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. – TheCodingArt Oct 25 '15 at 18:20
  • @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! – lol Oct 26 '15 at 03:00
  • 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 – Christopher Swasey Oct 30 '16 at 14:22
0

If you want to stretch a UINavigationBar with a custom background-image behind the UIStatusBar in iOS 7 consider the following:

  1. The UIStatusBar is transparent as it is.
  2. Set the barPosition property of the UINavigationBar to UIBarPositionTopAttached
  3. The UINavigationBar background-images in iOS 7 (if UIBarPositionTopAttached) have different dimensions than previous to iOS 7 and you have to use them: now the height is 64 points

In code (iPhone ONLY):

// Image needs 64 points height
NSString* navBarPortraitBackgroundPath = [[NSBundle mainBundle] pathForResource:@"navBarPortraitBackground" ofType:@"png"];
NSString* navBarLandscapeBackgroundPath;


if(UIScreen.mainScreen.bounds.size.height == 568){

    // Image needs 64 points height
    navBarLandscapeBackgroundPath = [[NSBundle mainBundle] pathForResource:@"navBarWideLandscapeBackground" ofType:@"png"];

} else {

    // Image needs 64 points height
    navBarLandscapeBackgroundPath = [[NSBundle mainBundle] pathForResource:@"navBarLandscapeBackground" ofType:@"png"];

}


[[UINavigationBar appearance] setBackgroundImage:[UIImage imageWithContentsOfFile:navBarPortraitBackgroundPath] forBarPosition:UIBarPositionTopAttached barMetrics:UIBarMetricsDefault];   
[[UINavigationBar appearance] setBackgroundImage:[UIImage imageWithContentsOfFile:navBarLandscapeBackgroundPath] forBarPosition:UIBarPositionTopAttached barMetrics:UIBarMetricsLandscapePhone];

If you just want to change to background colour of the UINavigationBar it will automatically extend behind the UIStatusBar.

In Code:

    [UINavigationBar appearance].barTintColor = [UIColor redColor];
Jarig
  • 1,087
  • 7
  • 7