【问题标题】:Get height of the safe area bottom inset from a keyboard extension从键盘扩展中获取安全区域底部插图的高度
【发布时间】:2021-07-09 17:23:07
【问题描述】:
从键盘扩展中,我们试图获取键盘下方空间的高度,当有一个主页指示器时。所有常用技术要么无法通过键盘扩展访问,要么返回 0。
从UIInputViewController,view.safeAreaInsets.bottom 返回 0,以及 parent!.view.safeAreaInsets.bottom
并且任何调用windows、UIApplication等的方法都无法从扩展中访问。
【问题讨论】:
标签:
ios
swift
objective-c
uikit
【解决方案1】:
Apple 已确认目前无法从键盘扩展中获知安全区域插入。
【解决方案2】:
如果是windows,UIApplication可以通过扩展访问你可以使用以下方法:
if #available(iOS 11.0, *)
{
let window = UIApplication.shared.keyWindow
let topPadding = window?.safeAreaInsets.top
let bottomPadding = window?.safeAreaInsets.bottom
}
if #available(iOS 13.0, *)
{
let window = UIApplication.shared.windows[0]
let topPadding = window.safeAreaInsets.top
let bottomPadding = window.safeAreaInsets.bottom
}
如果是windows,UIApplication不能通过扩展访问你可以使用以下方法:
使用 safeAreaLayoutGuide 属性。我创建了以下扩展,您可以使用它:
extension UIView {
public var safeAreaFrame: CGRect {
if #available(iOS 11, *) {
return safeAreaLayoutGuide.layoutFrame
}
return bounds
}
}
用法:
let frame = self.view.safeAreaFrame
print(frame)