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

前端 未结 2 1795
借酒劲吻你
借酒劲吻你 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];
    
    0 讨论(0)
  • 2020-12-31 20:33

    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

    0 讨论(0)
提交回复
热议问题