Status bar and navigation bar appear over my view's bounds in iOS 7

后端 未结 20 1644
既然无缘
既然无缘 2020-11-22 07:09

I recently downloaded Xcode 5 DP to test my apps in iOS 7. The first thing I noticed and confirmed is that my view\'s bounds is not always resized to account for the status

相关标签:
20条回答
  • 2020-11-22 07:53

    I would like to expand on Stunner's answer, and add an if statement to check if it is iOS-7, because when I tested it on iOS 6 my app would crash.

    The addition would be adding:

    if ([[[UIDevice currentDevice] systemVersion] floatValue] >= 7.0)
    

    So I would suggest adding this method to your MyViewControler.m file:

    - (void) viewDidLayoutSubviews {
        if ([[[UIDevice currentDevice] systemVersion] floatValue] >= 7.0) {
            CGRect viewBounds = self.view.bounds;
            CGFloat topBarOffset = self.topLayoutGuide.length;
            viewBounds.origin.y = topBarOffset * -1;
            self.view.bounds = viewBounds;
        }
    }
    
    0 讨论(0)
  • 2020-11-22 07:54

    You don't have to calculate how far to shift everything down, there's a build in property for this. In Interface Builder, highlight your view controller, and then navigate to the attributes inspector. Here you'll see some check boxes next to the words "Extend Edges". As you can see, in the first screenshot, the default selection is for content to appear under top and bottom bars, but not under opaque bars, which is why setting the bar style to not translucent worked for you.

    As you can somewhat see in the first screenshot, there are two UI elements hiding below the navigation bar. (I've enabled wireframes in IB to illustrate this) These elements, a UIButton and a UISegmentedControl both have their "y" origin set to zero, and the view controller is set to allow content below the top bar.

    enter image description here

    This second screenshot shows what happens when you deselect the "Under Top Bars" check box. As you can see, the view controllers view has been shifted down appropriately for its y origin to be right underneath the navigation bar.

    enter image description here

    This can also be accomplished programmatically through the usage of -[UIViewController edgesForExtendedLayout]. Here's a link to the class reference for edgeForExtendedLayout, and for UIRectEdge

    [self setEdgesForExtendedLayout:UIRectEdgeNone];
    
    0 讨论(0)
  • 2020-11-22 07:55

    I had the same issue with my app by iPads (armv7, armv7s, amr64) only by presenting another UIViewController and after dismiss them goes nav bar under status bar... I spend a lot of time to find any solution for that. I'm using storyboard and in InterfaceBuilder for UIViewController which makes terrible i set Presentation from FullScreen -> Current Context and it fix this issue. It works in my app only for iPads => iOS8.0 (testing with iOS8.1) and for iPads with iOS 7.1 not work!! see screenshot

    0 讨论(0)
  • 2020-11-22 07:56

    In my case having loadView() interrupted
    this code: self.edgesForExtendedLayout = UIRectEdgeNone

    but after deleting loadView() everything worked fine

    0 讨论(0)
  • 2020-11-22 07:57

    Swift Solution:

    override func viewWillAppear(animated: Bool) {
        super.viewWillAppear(animated)
        self.edgesForExtendedLayout = UIRectEdge.None
    }
    
    0 讨论(0)
  • 2020-11-22 07:57

    Swift 4.2 - Xcode 10.0 - iOS 12.0:

    if #available(iOS 11.0, *) {} else {
      self.edgesForExtendedLayout = []
      self.navigationController?.view.backgroundColor = .white
    }
    
    0 讨论(0)
提交回复
热议问题