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

前端 未结 2 1802
借酒劲吻你
借酒劲吻你 2020-12-31 20:10

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

2条回答
  •  无人及你
    2020-12-31 20:29

    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];
    

提交回复
热议问题