【发布时间】:2018-08-04 08:07:32
【问题描述】:
我在屏幕底部有一个文本视图,在屏幕顶部有一个搜索栏。以下是我在按下 textview 时解决键盘问题的代码
extension UIView {
func bindToKeyboard(){
NotificationCenter.default.addObserver(self, selector: #selector(UIView.keyboardWillChange(_:)), name: NSNotification.Name.UIKeyboardWillChangeFrame, object: nil)
}
@objc func keyboardWillChange(_ notification: NSNotification) {
let duration = notification.userInfo![UIKeyboardAnimationDurationUserInfoKey] as! Double
let curve = notification.userInfo![UIKeyboardAnimationCurveUserInfoKey] as! UInt
let curFrame = (notification.userInfo![UIKeyboardFrameBeginUserInfoKey] as! NSValue).cgRectValue
let targetFrame = (notification.userInfo![UIKeyboardFrameEndUserInfoKey] as! NSValue).cgRectValue
let deltaY = targetFrame.origin.y - curFrame.origin.y
UIView.animateKeyframes(withDuration: duration, delay: 0.0, options: UIViewKeyframeAnimationOptions(rawValue: curve), animations: {
self.frame.origin.y += deltaY
},completion: {(true) in
self.layoutIfNeeded()
})
}
}
但是当我按下搜索栏时,屏幕向上移动,搜索栏消失。如果我这样做view.bindToKeyboard(),那么在显示键盘后编辑文本是正确的。
我尝试过的一个解决方案是将 textview 的出口绑定到键盘,但是一旦我开始输入,textview 就会消失。
【问题讨论】:
-
你在用什么 UISearchBar 或 UISearchBarController?将您的配置放入搜索栏。
-
@guru 我正在使用 UISearchBar
-
能否分享截图或小视频,让我们看看实际行为。
-
@guru 即将上传