Get safe area inset top and bottom heights

后端 未结 15 1418
长发绾君心
长发绾君心 2020-11-30 17:30

On the new iPhone X, what would be the most proper way to get both top and bottom height for the unsafe areas?

15条回答
  •  隐瞒了意图╮
    2020-11-30 17:41

    All of the answers here are helpful, Thanks to everyone who offered help.

    However as i see that that the safe area topic is a little bit confused which won’t appear to be well documented.

    So i will summarize it here as mush as possible to make it easy to understand safeAreaInsets, safeAreaLayoutGuide and LayoutGuide.

    In iOS 7, Apple introduced the topLayoutGuide and bottomLayoutGuide properties in UIViewController, They allowed you to create constraints to keep your content from being hidden by UIKit bars like the status, navigation or tab bar It was possible with these layout guides to specify constraints on content, avoiding it to be hidden by top or bottom navigation elements (UIKit bars, status bar, nav or tab bar…).

    So for example if you wanna make a tableView starts from the top screen you have done something like that:

    self.tableView.contentInset = UIEdgeInsets(top: -self.topLayoutGuide.length, left: 0, bottom: 0, right: 0)
    

    In iOS 11 Apple has deprecated these properties replacing them with a single safe area layout guide

    Safe area according to Apple

    Safe areas help you place your views within the visible portion of the overall interface. UIKit-defined view controllers may position special views on top of your content. For example, a navigation controller displays a navigation bar on top of the underlying view controller’s content. Even when such views are partially transparent, they still occlude the content that is underneath them. In tvOS, the safe area also includes the screen’s overscan insets, which represent the area covered by the screen’s bezel.

    Below, a safe area highlighted in iPhone 8 and iPhone X-series:

    The safeAreaLayoutGuide is a property of UIView

    To get the height of safeAreaLayoutGuide:

    extension UIView {
       var safeAreaHeight: CGFloat {
           if #available(iOS 11, *) {
            return safeAreaLayoutGuide.layoutFrame.size.height
           }
           return bounds.height
      }
    }
    

    That will return the height of the Arrow in your picture.

    Now, what about getting the top "notch" and bottom home screen indicator heights?

    Here we will use the safeAreaInsets

    The safe area of a view reflects the area not covered by navigation bars, tab bars, toolbars, and other ancestors that obscure a view controller's view. (In tvOS, the safe area reflects the area not covered by the screen's bezel.) You obtain the safe area for a view by applying the insets in this property to the view's bounds rectangle. If the view is not currently installed in a view hierarchy, or is not yet visible onscreen, the edge insets in this property are 0.

    The following will show the unsafe area and there distance from edges on iPhone 8 and one of iPhone X-Series.

    Now, if navigation bar added

    So, now how to get the unsafe area height? we will use the safeAreaInset

    Here are to solutions however they differ in an important thing,

    First One:

    self.view.safeAreaInsets
    

    That will return the EdgeInsets, you can now access the top and the bottom to know the insets,

    Second One:

    UIApplication.shared.windows.first{$0.isKeyWindow }?.safeAreaInsets
    

    The first one you are taking the view insets, so if there a navigation bar it will be considered , however the second one you are accessing the window's safeAreaInsets so the navigation bar will not be considered

提交回复
热议问题