【问题标题】:Get height of the safe area bottom inset from a keyboard extension从键盘扩展中获取安全区域底部插图的高度
【发布时间】:2021-07-09 17:23:07
【问题描述】:

从键盘扩展中,我们试图获取键盘下方空间的高度,当有一个主页指示器时。所有常用技术要么无法通过键盘扩展访问,要么返回 0

UIInputViewControllerview.safeAreaInsets.bottom 返回 0,以及 parent!.view.safeAreaInsets.bottom

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

【问题讨论】:

  • 你用过AssistantKit吗?

标签: 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)
    

    【讨论】:

    • 谢谢;如问题中所述,无法从扩展程序访问
    猜你喜欢
    • 2018-03-31
    • 2018-01-06
    • 2022-08-03
    • 2018-04-28
    • 1970-01-01
    • 2021-05-03
    • 2020-07-21
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多