iPhone X 上的额外底部 space/padding?
Extra bottom space/padding on iPhone X?
在纵向模式下的 iPhone X 上,如果将安全区域的底部约束设置为 0,最终会在屏幕底部出现一个额外的 space。
你如何以编程方式获得这个额外填充的高度?
我设法手动确定了此填充的高度,即 34,这是我如何使用 iPhone X 检测实现它的方法:
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
}
}
是否有更简洁的方法来检测此填充的高度?
在iOS11,观点有一个safeAreaInsets
属性。如果你得到这些插图的 bottom
属性 你可以在 iPhone X:
上得到底部填充的高度
if #available(iOS 11.0, *) {
let bottomPadding = view.safeAreaInsets.bottom
// ...
}
(同样适用于带有状态栏的顶部填充)
如果您突然没有视图,例如 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
}
在Objective-C
if (@available(iOS 11.0, *)) {
UIWindow *window = UIApplication.sharedApplication.keyWindow;
CGFloat bottomPadding = window.safeAreaInsets.bottom;
}
用这条线成为你iPhoneX的底价
if #available(iOS 11.0, *) {
let bottomPadding = view.safeAreaInsets.bottom
}
并且不要忘记在 layoutSubviews() 中添加此 因为 safeAreaInsets 在 layoutSubviews() 中具有正确的大小,否则你将成为错误的值。
var bottomPadding: CGFloat = 0.0
if #available(iOS 11.0, *) {
let window = UIApplication.shared.keyWindow
bottomPadding = window?.safeAreaInsets.bottom ?? 0.0
}
现在您可以根据需要使用bottomPadding
。
iOS 11(上述答案的混合)
let padding = UIApplication.shared.keyWindow?.safeAreaInsets.bottom ?? 0
UIApplication.shared.windows.first?.safeAreaInsets.bottom ?? 0.0
iOS 13 岁及以上
在纵向模式下的 iPhone X 上,如果将安全区域的底部约束设置为 0,最终会在屏幕底部出现一个额外的 space。 你如何以编程方式获得这个额外填充的高度?
我设法手动确定了此填充的高度,即 34,这是我如何使用 iPhone X 检测实现它的方法:
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
}
}
是否有更简洁的方法来检测此填充的高度?
在iOS11,观点有一个safeAreaInsets
属性。如果你得到这些插图的 bottom
属性 你可以在 iPhone X:
if #available(iOS 11.0, *) {
let bottomPadding = view.safeAreaInsets.bottom
// ...
}
(同样适用于带有状态栏的顶部填充)
如果您突然没有视图,例如 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
}
在Objective-C
if (@available(iOS 11.0, *)) {
UIWindow *window = UIApplication.sharedApplication.keyWindow;
CGFloat bottomPadding = window.safeAreaInsets.bottom;
}
用这条线成为你iPhoneX的底价
if #available(iOS 11.0, *) {
let bottomPadding = view.safeAreaInsets.bottom
}
并且不要忘记在 layoutSubviews() 中添加此 因为 safeAreaInsets 在 layoutSubviews() 中具有正确的大小,否则你将成为错误的值。
var bottomPadding: CGFloat = 0.0
if #available(iOS 11.0, *) {
let window = UIApplication.shared.keyWindow
bottomPadding = window?.safeAreaInsets.bottom ?? 0.0
}
现在您可以根据需要使用bottomPadding
。
iOS 11(上述答案的混合)
let padding = UIApplication.shared.keyWindow?.safeAreaInsets.bottom ?? 0
UIApplication.shared.windows.first?.safeAreaInsets.bottom ?? 0.0
iOS 13 岁及以上