ios iPhone X 上的额外底部空间/填充?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/46239960/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-31 09:28:39  来源:igfitidea点击:

Extra bottom space/padding on iPhone X?

iosiphoneswiftiphone-x

提问by standousset

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 ?

在 iPhone X 的竖屏模式下,如果你将安全区域的底部约束设置为 0,你最终会在屏幕底部获得额外的空间。你如何以编程方式获得这个额外填充的高度?

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:

我设法手动确定了这个填充的高度,即 34,这是我如何通过 iPhone X 检测来实现它:

Swift 4.0 and Xcode 9.0

Swift 4.0 和 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 ?

有没有更干净的方法来检测这个填充的高度?

回答by Paolo

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

在 iOS 11 中,视图有一个safeAreaInsets属性。如果您获得bottom这些插图的属性,您可以在 iPhone X 上获得底部填充的高度:

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

(likewise for the top padding with status bar)

(同样对于带有状态栏的顶部填充)

回答by user6788419

In Objective-C

Objective-C 中

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

回答by Hemang

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 bottomPaddingas per your needs.

现在您可以bottomPadding根据需要使用。

回答by Azon

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

如果您突然没有视图,例如 CollectionViewLayout,您也可以从 Window 中检索它:

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
}

回答by Merricat

iOS 11(a mix from answers above)

iOS 11(以上答案的组合)

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

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

回答by Osman

use this line to become your bottom value for iPhoneX

用这条线成为你对 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.

并且不要忘记在 layoutSubviews() 中添加它 因为 safeAreaInsets在 layoutSubviews() 中具有正确的大小,否则您将成为错误的值。

回答by user10447655

UIApplication.shared.windows.first?.safeAreaInsets.bottom ?? 0.0

iOS 13 and up

iOS 13 及更高版本