从键盘扩展中获取安全区域底部插图的高度

Get height of the safe area bottom inset from a keyboard extension

从键盘扩展中,当有主页指示器时,我们试图获取键盘下方 space 的高度。所有常用技术要么无法从键盘扩展访问,要么 return 0.

来自UIInputViewControllerview.safeAreaInsets.bottom、returns 0,以及parent!.view.safeAreaInsets.bottom

并且任何调用 windows、UIApplication 等的方法都无法从扩展中访问。

如果 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)

Apple 已确认目前无法从键盘扩展中获知安全区域。