ios - UIView 在显示键盘时出现,但在我开始输入时消失(swift)?
问题描述
我有一个UIView
装有 aUITextField
和两个的容器UIButtons
(见下图)。
我写了一个extension
监听UIKeyboardWillShow
通知并动画, UIView
以便它在显示时绑定到键盘。
这非常有效,但是一旦我开始输入,它就会UIView
再次消失。
扩展用法:
sendMessageView.bindToKeyboard()
UIView 扩展:
extension UIView {
func bindToKeyboard() {
NotificationCenter.default.addObserver(self, selector: #selector(keyboardWillShow), name: NSNotification.Name.UIKeyboardWillShow, object: nil)
}//end func
@objc func keyboardWillShow(notification: NSNotification) {
let duration = notification.userInfo![UIKeyboardAnimationDurationUserInfoKey] as! Double
let curve = notification.userInfo![UIKeyboardAnimationCurveUserInfoKey] as! UInt
let beginningFrame = (notification.userInfo![UIKeyboardFrameBeginUserInfoKey] as! NSValue).cgRectValue
let endFrame = (notification.userInfo![UIKeyboardFrameEndUserInfoKey] as! NSValue).cgRectValue
let deltaY = endFrame.origin.y - beginningFrame.origin.y
UIView.animateKeyframes(withDuration: duration, delay: 0.0, options: UIViewKeyframeAnimationOptions(rawValue: curve), animations: {
self.frame.origin.y += deltaY
}, completion: nil)
}//end func
}//end extension
UIView 在显示时绑定到键盘:
键入时 UIView 消失(我怀疑在键盘后面到其原始位置):
解决方案
不要为 UIView 本身添加扩展名。只需将您的 textBox 视图添加为子视图,并将视图的底部约束设置为根据键盘的高度进行更新。如果键盘存在,则将常量设置为键盘高度的负值,否则将其设置为零。
private func setupView() {
NotificationCenter.default.addObserver(self, selector: #selector(keyBoardShown(_:)), name: UIResponder.keyboardWillShowNotification, object: nil)
self.view.backgroundColor = .white
self.view.addSubview(textInputView)
bottomConstraint = NSLayoutConstraint(item: textInputView, attribute: .bottom, relatedBy: .equal, toItem: view.safeAreaLayoutGuide, attribute: .bottom, multiplier: 1.0, constant: 0)
NSLayoutConstraint.activate([
//textBox.bottomAnchor.constraint(equalTo: view.bottomAnchor, constant: 0),
//textInputView.heightAnchor.constraint(equalToConstant: UIFont.systemFontSize+14+10),
textInputView.leadingAnchor.constraint(equalTo: view.safeAreaLayoutGuide.leadingAnchor, constant: 0),
textInputView.trailingAnchor.constraint(equalTo: view.safeAreaLayoutGuide.trailingAnchor, constant: 0)
])
bottomConstraint.isActive = true
}
@objc private func keyBoardShown(_ notification: NSNotification) {
if let keyboardSize = (notification.userInfo?[UIResponder.keyboardFrameEndUserInfoKey] as? NSValue)?.cgRectValue {
print(keyboardSize.height)
bottomConstraint.constant = -keyboardSize.height
UIView.animate(withDuration: 0.5,
delay: TimeInterval(0),
options: .beginFromCurrentState,
animations: { self.view.layoutIfNeeded() },
completion: nil)
}
}
推荐阅读
- ios - Swift:尽管我调用了所需的代码,但导航栏却隐藏了?
- heroku - 获取 heroku 数据库地址和凭据,而不是 DATABASE_URL
- c - 如何使用 PWM 实现职责?
- google-sheets - 谷歌表:使用正则表达式过滤文字问号字符
- twig - Twig {% spaceless %} 如果变量为真
- java - RxJava2:onErrorResumeNext 不拦截 400 响应代码
- javascript - 在 React 中将状态从一个组件设置到另一个组件的最佳实践方法
- c++ - 从 'pthread_t' 到 'pthread_t* {aka long unsigned int}' 到 'pthread_t* 的 C++ 无效转换
- c++ - 嗨... id_public 变量不应该给出结果为 81 和 86 吗?为什么它为两个派生类占用不同的内存位置?
- visual-studio-code - 编译时未定义对“WinMain@16”的引用