处理另一个应用程序';iPad分割视图上的模糊键盘(iOS 9多任务)

处理另一个应用程序';iPad分割视图上的模糊键盘(iOS 9多任务),ios,ipad,keyboard,multitasking,splitview,Ios,Ipad,Keyboard,Multitasking,Splitview,以前,如果一个人在自己的应用程序上展示键盘,他会将所有内容嵌入UIScrollView中,并调整contentInset,以防止内容被键盘遮挡 现在,在iOS 9上使用拆分视图多任务处理,即使用户不再与其他应用程序交互,键盘也可能随时出现并保持可见 问题 有没有一种简单的方法可以调整所有不希望键盘可见的视图控制器,而不开始在ScrollView中嵌入所有内容?秘密在于,只要在应用程序或运行的其他应用程序中显示/隐藏键盘,就会触发UIKeyboardWillChangeFrame通知和你的肩并肩

以前,如果一个人在自己的应用程序上展示键盘,他会将所有内容嵌入
UIScrollView
中,并调整
contentInset
,以防止内容被键盘遮挡

现在,在iOS 9上使用拆分视图多任务处理,即使用户不再与其他应用程序交互,键盘也可能随时出现并保持可见

问题


有没有一种简单的方法可以调整所有不希望键盘可见的视图控制器,而不开始在ScrollView中嵌入所有内容?

秘密在于,只要在应用程序或运行的其他应用程序中显示/隐藏键盘,就会触发
UIKeyboardWillChangeFrame
通知和你的肩并肩

我创建了这个扩展,以便于开始/停止观察这些事件(我在
视图中将它们称为willbeen
/
invision
),并轻松获得
模糊高度,该高度通常用于调整表/集合/滚动视图的底部
contentInset

@objc protocol KeyboardObserver
{
    func startObservingKeyboard() // Call this in your controller's viewWillAppear
    func stopObservingKeyboard() // Call this in your controller's viewWillDisappear
    func keyboardObscuredHeight() -> CGFloat
    @objc optional func adjustLayoutForKeyboardObscuredHeight(_ obscuredHeight: CGFloat, keyboardFrame: CGRect, keyboardWillAppearNotification: Notification) // Implement this in your controller and adjust your bottom inset accordingly
}

var _keyboardObscuredHeight:CGFloat = 0.0;

extension UIViewController: KeyboardObserver
{
    func startObservingKeyboard()
    {
        NotificationCenter.default.addObserver(self, selector: #selector(observeKeyboardWillChangeFrameNotification(_:)), name: NSNotification.Name.UIKeyboardWillChangeFrame, object: nil)
    }

    func stopObservingKeyboard()
    {
        NotificationCenter.default.removeObserver(self, name: NSNotification.Name.UIKeyboardWillChangeFrame, object: nil)
    }

    func observeKeyboardWillChangeFrameNotification(_ notification: Notification)
    {
        guard let window = self.view.window else {
            return
        }

        let animationID = "\(self) adjustLayoutForKeyboardObscuredHeight"
        UIView.beginAnimations(animationID, context: nil)
        UIView.setAnimationCurve(UIViewAnimationCurve(rawValue: (notification.userInfo![UIKeyboardAnimationCurveUserInfoKey]! as AnyObject).intValue)!)
        UIView.setAnimationDuration((notification.userInfo![UIKeyboardAnimationCurveUserInfoKey]! as AnyObject).doubleValue)

        let keyboardFrame = (notification.userInfo![UIKeyboardFrameEndUserInfoKey]! as AnyObject).cgRectValue
        _keyboardObscuredHeight = window.convert(keyboardFrame!, from: nil).intersection(window.bounds).size.height
        let observer = self as KeyboardObserver
        observer.adjustLayoutForKeyboardObscuredHeight!(_keyboardObscuredHeight, keyboardFrame: keyboardFrame!, keyboardWillAppearNotification: notification)

        UIView.commitAnimations()
    }

    func keyboardObscuredHeight() -> CGFloat
    {
        return _keyboardObscuredHeight
    }
}

你能找到这个问题的答案吗?添加了我的解决方案。当应用程序启动时,键盘已经存在,这会发现这种情况吗?如果没有,你是怎么解决的?我调用
键盘遮挡高度()
一旦打开
视图就会出现
@PeterJohnson。