gpt4 book ai didi

用于底部 TextView 和顶部搜索栏的 IOS 键盘

转载 作者:可可西里 更新时间:2023-11-01 05:48:38 25 4
gpt4 key购买 nike

我在屏幕底部有一个 TextView ,在屏幕顶部有一个搜索栏。以下是我解决按下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 的导出绑定(bind)到键盘,但一旦我开始输入,textview 就会消失。

最佳答案

我认为问题在于您试图知道键盘何时会出现。搜索栏有一个文本字段。因此,当它被点击时,它会像您的 TextView 一样打开键盘,并调用 keyboardWillChange

   keyboardWillChange(_ notification: NSNotification)

因此键盘出现并隐藏您的搜索栏。您可以检测搜索栏点击并取消那里的通知。

    func searchBarShouldBeginEditing(_ searchBar: UISearchBar) -> Bool
{
//Dismiss your keyboard notification here

return true
}

您应该使用库来处理这种情况。我建议使用 IQKeyboardManagerIQKeyBoardManager Github.

关于用于底部 TextView 和顶部搜索栏的 IOS 键盘,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51683720/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com