gpt4 book ai didi

swift - 当我点击 TextView 两次时,键盘将显示通知不起作用

转载 作者:行者123 更新时间:2023-11-30 11:16:54 25 4
gpt4 key购买 nike

我在屏幕顶部有一个 TextView ,在屏幕底部有一个自定义 View (如工具栏)。当用户单击 TextView 时,键盘就会出现,工具栏也会随着键盘一起上升。然后我在工具栏上有一个关闭按钮,它调用 TextView 上的结束编辑函数。当我第二次单击 TextView 时,工具栏不会出现,但我需要它出现。这是我附加到自定义工具栏上的代码,使其向上移动:

extension UIView{
func bindToKeyboard(){
NotificationCenter.default.addObserver(self, selector: #selector(keyboardWillShow(_:)), name: NSNotification.Name.UIKeyboardWillShow, object: nil)
NotificationCenter.default.addObserver(self, selector: #selector(keyboardWillHide(_:)), name: NSNotification.Name.UIKeyboardWillHide, object: nil)
}

@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: {

if UIDevice().userInterfaceIdiom == .phone && UIScreen.main.nativeBounds.height == 2436 {//check user device
//iPhone X
self.frame.origin.y += deltaY + 35
}else{
self.frame.origin.y += deltaY
}

}, completion: nil)
}

@objc func keyboardWillHide(_ 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: {

if UIDevice().userInterfaceIdiom == .phone && UIScreen.main.nativeBounds.height == 2436 {//check user device
//iPhone X

self.frame.origin.y = 0
}else{
self.frame.origin.y = 0
}

}, completion: nil)
}
}

这是键盘出现时我的 TextView 的代码:

 override func viewDidLoad() {
super.viewDidLoad()

self.editView.bindToKeyboard()

NotificationCenter.default.addObserver(self, selector: #selector(keyboardWillShow(_:)), name: NSNotification.Name.UIKeyboardWillChangeFrame, object: nil)
NotificationCenter.default.addObserver(self, selector: #selector(keyboardWillHide(_:)), name: NSNotification.Name.UIKeyboardWillHide, object: nil)
}



@objc func keyboardWillShow(_ notification: NSNotification) {
if let keyboardSize = (notification.userInfo?[UIKeyboardFrameBeginUserInfoKey] as? NSValue)?.cgRectValue {
let contentInsets = UIEdgeInsets(top: 0, left: 0, bottom: keyboardSize.height, right: 0)
textView.contentInset = contentInsets
}
}

@objc func keyboardWillHide(_ notification: NSNotification) {

let contentInsets = UIEdgeInsets(top: 0, left: 0, bottom: 0, right: 0)
textView.contentInset = contentInsets

}

最佳答案

对于键盘显示状态,请使用 UIKeyboardWillShowNotification。

   NotificationCenter.default.addObserver(self, selector:#selector(keyboardWillShow) , name: UIResponder.keyboardWillShowNotification, object: nil)

关于swift - 当我点击 TextView 两次时,键盘将显示通知不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51659513/

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