sta*_*set 41 iphone ios swift iphone-x
在纵向模式下的iPhone X上,如果将安全区域的底部约束设置为0,则最终会在屏幕底部留出额外的空间.你如何以编程方式获得这个额外填充的高度?
我设法手动确定这个填充的高度是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
}
}
Run Code Online (Sandbox Code Playgroud)
有没有更清晰的方法来检测这个填充的高度?
Pao*_*olo 65
在iOS 11中,视图具有safeAreaInsets属性.如果你获得了bottom这些插图的属性,你可以在iPhone X上获得底部填充的高度:
if #available(iOS 11.0, *) {
let bottomPadding = view.safeAreaInsets.bottom
// ...
}
Run Code Online (Sandbox Code Playgroud)
(同样用于带状态栏的顶部填充)
use*_*419 39
在Objective-C中
if (@available(iOS 11.0, *)) {
UIWindow *window = UIApplication.sharedApplication.keyWindow;
CGFloat bottomPadding = window.safeAreaInsets.bottom;
}
Run Code Online (Sandbox Code Playgroud)
var bottomPadding: CGFloat = 0.0
if #available(iOS 11.0, *) {
let window = UIApplication.shared.keyWindow
bottomPadding = window?.safeAreaInsets.bottom ?? 0.0
}
Run Code Online (Sandbox Code Playgroud)
现在,您可以bottomPadding根据需要使用。
小智 6
如果突然没有视图,例如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
}
Run Code Online (Sandbox Code Playgroud)
iOS 11(以上答案的组合)
let padding = UIApplication.shared.keyWindow?.safeAreaInsets.bottom ?? 0
小智 5
UIApplication.shared.windows.first?.safeAreaInsets.bottom ?? 0.0
Run Code Online (Sandbox Code Playgroud)
iOS 13 及更高版本
| 归档时间: |
|
| 查看次数: |
34061 次 |
| 最近记录: |