[英]UIView appears when keyboard is displayed but then disappears when I start to type (swift)?
I have a UIView
container which holds a UITextField
and two UIButtons
(see image below). 我有一个
UIView
容器,其中包含一个UITextField
和两个UIButtons
(请参见下图)。
I have written an extension
which listens to a UIKeyboardWillShow
notification and animates the UIView
so that it binds to the keyboard when it shows. 我编写了一个
extension
,它监听UIKeyboardWillShow
通知并为UIView
动画,以便它在显示时绑定到键盘。
This works perfectly, but as soon as I start typing the UIView
disappears again. 这完美地工作,但是一旦我开始输入,
UIView
再次消失。
Extension usage: 扩展程序用法:
sendMessageView.bindToKeyboard()
UIView extension: 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 binds to keyboard when it is displayed: UIView在显示时绑定到键盘:
UIView disappears (I suspect behind the keyboard to its original location) when typing: 输入以下内容时,UIView消失(我怀疑是在键盘后面到达其原始位置):
Don't put an extension for the UIView itself. 不要为UIView本身添加扩展。 Just add your textBox view as a subview and set the bottom constraint of your view to be updating according to the height of the keyboard.
只需将textBox视图添加为子视图,然后设置视图的底部约束即可根据键盘的高度进行更新。 If keyboard is present, set the constant to the negative value of height of the keyboard else set it to zero.
如果存在键盘,请将常数设置为键盘高度的负值,否则将其设置为零。
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)
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.