gpt4 book ai didi

ios - 在 iPad Split View上处理另一个应用程序的模糊键盘(iOS 9 多任务处理)

转载 作者:行者123 更新时间:2023-12-01 16:24:32 24 4
gpt4 key购买 nike

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

现在有了 iOS 9 上的 Split View多任务处理,键盘可能随时出现并保持可见,即使用户不再与其他应用程序交互。

问题

有没有一种简单的方法来调整所有不期望键盘可见并且不开始将所有内容嵌入到 ScrollView 中的 View Controller ?

最佳答案

秘诀就是听UIKeyboardWillChangeFrame每当您的应用程序或与您的应用程序并排运行的另一个应用程序显示/隐藏键盘时触发的通知。

我创建了这个扩展,以便于开始/停止观察这些事件(我在 viewWillAppear/Disappear 中调用它们),并轻松获得 obscuredHeight通常用于调整底部contentInset你的表/集合/ ScrollView 。

@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
}
}

关于ios - 在 iPad Split View上处理另一个应用程序的模糊键盘(iOS 9 多任务处理),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36650446/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com