iOS 7 Status Bar Collides With NavigationBar

前端 未结 17 1218
攒了一身酷
攒了一身酷 2020-11-28 03:14

I have a view controller in my app that has a navigation bar dragged on it in the storyboard. It was working fine in the iOS 6 but in iOS 7 it look like this:

相关标签:
17条回答
  • 2020-11-28 03:29

    it's the best answer. But I wanted know how to use a Storyboard and dragged UINavigationBar on it. When I implemented the delegate method, and set the return result to UIBarPositionTopAttached, it did not work.

    - (void)viewDidLoad{
        self.navigationbar.delegate = self;
    }
    - (UIBarPosition)positionForBar:(id<UIBarPositioning>)bar{
        NSLog(@"Got it");
        //
        //    CGRect frame = self.navigaitonBar.frame;
        //
        //    frame = CGRectMake(0, 20, CGRectGetWidth(frame), CGRectGetHeight(frame));
        //    self.navigaitonBar.frame = frame;
        //
        //    NSLog(@"frame %f",frame.origin.y);
        return UIBarPositionTopAttached;
    }
    
    0 讨论(0)
  • 2020-11-28 03:30

    This works for me i hope you also have same luck :).
    Add below code in your view.

    -(void) viewDidLayoutSubviews
    {
        CGRect tmpFram = self.navigationController.navigationBar.frame;
        tmpFram.origin.y += 20;
        self.navigationController.navigationBar.frame = tmpFram;
    }
    

    It basically change location of navigation bar.

    0 讨论(0)
  • 2020-11-28 03:30

    If your UIViewController is NOT in a UINavigationController and you're using UIStoryBoard, you can set the "iOS 6/7 Deltas" to 20 for the delta Y, for every subview that needs to be offset from the UIStatusBar.

    enter image description here

    0 讨论(0)
  • 2020-11-28 03:32

    In case it still helps someone, this is what worked for me for moving the Navigation Bar little bit down in ios 7 and above:

    -(void)viewWillLayoutSubviews
    {
        float iosVersion = 7.0;
        if ([[[UIDevice currentDevice] systemVersion] floatValue] >= iosVersion) {
            // iOS 7+
            CGRect viewFrame = self.view.frame;
            viewFrame.origin.y += 10;
            self.view.frame = viewFrame;
        }
    }
    

    On a device with ios 6.1 and below the Navigation Bar will be unchanged, as it was before.

    And this is what I used to make the contents of the Status Bar lighter:

    -(UIStatusBarStyle)preferredStatusBarStyle{
        return UIStatusBarStyleLightContent;
    }
    
    0 讨论(0)
  • 2020-11-28 03:34

    The navigation bar is too close to the status bar because starting in iOS 7, the status bar is more of an overlay over the whole view controller's view. Since your navigation bar is at (0, 0), the status bar will show on top of the navigation bar. To solve this, simply move the navigation bar down (or, as others have said), create a constraint between the navigation bar and the topLayoutGuide.

    When you do that, you will see that there is now a 20 point gap between the navigation bar and the top of the screen. That's because you just moved the navigation bar down 20 points. "But UINavigationController can do it right!" Absolutely, and it does so by implementing UIBarPositioningDelegate on your view controller. This is a one-method protocol that should be implemented like this:

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

    After adding your view controller as the delegate for the navigation bar, you'll notice the navigation bar is still shifted down 20 points, but its background will extend up underneath the status bar, just like in UINavigationController.

    Another thing you're seeing is that the navigation bar is translucent, meaning anything underneath the navigation bar will be visible to some extent. The translucent property on UINavigationBar is set to YES by default on iOS 7. Before iOS 7, the default was NO.

    0 讨论(0)
  • 2020-11-28 03:40

    This is new feature with IOS7. Instead of staring at 20 px navigation bar in IOS7 staring at 0 px. As a solution shift the whole view downwards to 20 px or you can use image for navigation bar with height 64px.

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