Get safe area inset top and bottom heights

2019-01-16 07:10发布

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

enter image description here

5条回答
爱情/是我丢掉的垃圾
2楼-- · 2019-01-16 07:32

None of the other answers here worked for me, but this did.

var topSafeAreaHeight: CGFloat = 0
var bottomSafeAreaHeight: CGFloat = 0

  if #available(iOS 11.0, *) {
    let window = UIApplication.shared.windows[0]
    let safeFrame = window.safeAreaLayoutGuide.layoutFrame
    topSafeAreaHeight = safeFrame.minY
    bottomSafeAreaHeight = window.frame.maxY - safeFrame.maxY
  }
查看更多
\"骚年 ilove
3楼-- · 2019-01-16 07:40

Swift 4

To pin a view to a safe area anchor using constraints can be done at any point in the view controller's lifecycle because constraints are handled by the auto layout API behind the scenes. However, to access the values of those safe areas to determine their heights requires waiting until later in the view controller's lifecycle, ideally viewDidLayoutSubviews().

This plugs into any view controller:

override func viewDidLayoutSubviews() {
    super.viewDidLayoutSubviews()

    var topSafeArea: CGFloat
    var bottomSafeArea: CGFloat

    if #available(iOS 11.0, *) {
        topSafeArea = view.safeAreaInsets.top
        bottomSafeArea = view.safeAreaInsets.bottom
    } else {
        topSafeArea = topLayoutGuide.length
        bottomSafeArea = bottomLayoutGuide.length
    }

    // safe area values are now available to use

}

I prefer this method to getting it off of the window because it’s how Apple designed the API to be used. Don't reference extraneous objects like the window when everything is already in the view controller. And by using viewDidLayoutSubviews(), safe area values are updated during orientation changes, double-height status bars, and all other mutations to the view and its safe areas.

查看更多
姐就是有狂的资本
4楼-- · 2019-01-16 07:43

Try this :

In Objective C

if (@available(iOS 11.0, *)) {
    UIWindow *window = UIApplication.sharedApplication.keyWindow;
    CGFloat topPadding = window.safeAreaInsets.top;
    CGFloat bottomPadding = window.safeAreaInsets.bottom;
}

In Swift

if #available(iOS 11.0, *) {
    let window = UIApplication.shared.keyWindow
    let topPadding = window?.safeAreaInsets.top
    let bottomPadding = window?.safeAreaInsets.bottom
}
查看更多
你好瞎i
5楼-- · 2019-01-16 07:44

To get the height between the layout guides you just do

let guide = view.safeAreaLayoutGuide
let height = guide.layoutFrame.size.height

So full frame height = 812.0, safe area height = 734.0

Below is the example where the green view has frame of guide.layoutFrame

enter image description here

查看更多
甜甜的少女心
6楼-- · 2019-01-16 07:44

I'm working with CocoaPods frameworks and in case UIApplication.shared is unavailable then I use safeAreaInsets in view's window:

if #available(iOS 11.0, *) {
    let insets = view.window?.safeAreaInsets
    let top = insets.top
    let bottom = insets.bottom
}
查看更多
登录 后发表回答