Extra bottom space/padding on iPhone X?

你。 提交于 2019-12-03 03:01:26

问题


On the iPhone X in portrait mode, if you set a bottom constraint to safe area to 0, you will end up with an extra space at the bottom of the screen. How do you get programmatically the height of this extra padding ?

I managed to manually determine the height of this padding which is 34 and here is how I managed to implement it with iPhone X detection:

Swift 4.0 and Xcode 9.0

if UIDevice().userInterfaceIdiom == .phone
{
    switch UIScreen.main.nativeBounds.height
    {
        case 2436: //iPhone X
        self.keyboardInAppOffset.constant = -34.0
        default:
        self.keyboardInAppOffset.constant = 0
    }
}

Is there a cleaner way to detect the height of this padding ?


回答1:


In iOS 11, views have a safeAreaInsets property. If you get the bottom property of these insets you can get the height of the bottom padding while on iPhone X:

if #available(iOS 11.0, *) {
    let bottomPadding = view.safeAreaInsets.bottom
    // ...
}

(likewise for the top padding with status bar)




回答2:


In Objective-C

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



回答3:


var bottomPadding: CGFloat = 0.0
if #available(iOS 11.0, *) {
     let window = UIApplication.shared.keyWindow
     bottomPadding = window?.safeAreaInsets.bottom ?? 0.0
}

Now you can use bottomPadding as per your needs.




回答4:


If suddenly you have no view, for example, CollectionViewLayout, you can retrieve it from Window too:

private static var itemHeight: CGFloat {
    guard #available(iOS 11.0, *),
        let window = UIApplication.shared.keyWindow else {
            return Constants.itemHeight
    }
    return Constants.itemHeight - window.safeAreaInsets.bottom
}



回答5:


iOS 11 (a mix from answers above)

let padding = UIApplication.shared.keyWindow?.safeAreaInsets.bottom ?? 0




回答6:


use this line to become your bottom value for iPhoneX

if #available(iOS 11.0, *) {
            let bottomPadding = view.safeAreaInsets.bottom
  }

and don't forget to add this in layoutSubviews() because safeAreaInsets has the correct size in layoutSubviews() otherwise you will become wrong values.



来源:https://stackoverflow.com/questions/46239960/extra-bottom-space-padding-on-iphone-x

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!