在没有 IQKeyboardManagerSwift 或 IHKeyboardAvoiding 等框架的情况下,修复键盘重叠的最佳方法是什么?
最佳答案
设置 ScrollView 底部布局约束,
这使得所有文本字段都可以通过滚动使用,您无需考虑其他设备和键盘高度差异。
@IBOutlet weak var mainScrollViewBottomConstraint: NSLayoutConstraint!
open override func viewDidLoad() {
super.viewDidLoad()
let notificationCenter = NotificationCenter.default
notificationCenter.addObserver(self, selector: #selector(keyboardWillShow), name: NSNotification.Name.UIKeyboardWillShow, object: nil)
notificationCenter.addObserver(self, selector: #selector(keyboardWillHide), name: NSNotification.Name.UIKeyboardWillHide, object: nil)
}
func keyboardWillShow(_ notification: Notification) {
let userInfo = (notification as NSNotification).userInfo!
let keyboardSize = (userInfo[UIKeyboardFrameEndUserInfoKey] as! NSValue).cgRectValue
mainScrollViewBottomConstraint.constant = keyboardSize.height
}
func keyboardWillHide(_ notification: Notification) {
mainScrollViewBottomConstraint.constant = 0
}
编辑:我制作了一个关于我的解决方案的视频。 https://youtu.be/gS4AKcJAg3Y
关于ios - 快速处理键盘重叠,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44097684/