ios - UIView 在显示键盘时出现,但在我开始输入时消失(swift)?
<p><p>我有一个 <code>UIView</code> 容器,其中包含一个 <code>UITextField</code> 和两个 <code>UIButtons</code>(见下图)。</p>
<p>我编写了一个 <code>extension</code>,它监听 <code>UIKeyboardWillShow</code> 通知并为 <code>UIView</code> 设置动画,以便在显示时绑定(bind)到键盘。 </p>
<p><strong>这很好用,但是一旦我开始输入 <code>UIView</code> 就会再次消失。</strong></p>
<p>扩展用法:</p>
<pre><code>sendMessageView.bindToKeyboard()
</code></pre>
<p>UIView 扩展:</p>
<pre><code>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! as! Double
let curve = notification.userInfo! as! UInt
let beginningFrame = (notification.userInfo! as! NSValue).cgRectValue
let endFrame = (notification.userInfo! 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
</code></pre>
<p>UIView 在显示时绑定(bind)到键盘:</p>
<p> <a href="/image/K2AUN.jpg" rel="noreferrer noopener nofollow"><img src="/image/K2AUN.jpg" alt="enter image description here"/></a> </p>
<p>输入时 UIView 消失(我怀疑在键盘后面到其原始位置):</p>
<p> <a href="/image/chbjp.jpg" rel="noreferrer noopener nofollow"><img src="/image/chbjp.jpg" alt="enter image description here"/></a> </p></p>
<br><hr><h1><strong>Best Answer-推荐答案</ strong></h1><br>
<p><p>不要为 UIView 本身添加扩展名。只需将您的 textBoxView 添加为 subview ,并将 View 的底部约束设置为根据键盘的高度进行更新。如果存在键盘,则将常量设置为键盘高度的负值,否则将其设置为零。</p>
<pre><code> 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? 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)
}
}
</code></pre></p>
<p style="font-size: 20px;">关于ios - UIView 在显示键盘时出现,但在我开始输入时消失(swift)?,我们在Stack Overflow上找到一个类似的问题:
<a href="https://stackoverflow.com/questions/51604788/" rel="noreferrer noopener nofollow" style="color: red;">
https://stackoverflow.com/questions/51604788/
</a>
</p>
页:
[1]