gpt4 book ai didi

键盘出现时屏幕快速移动

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

我的屏幕上有一个 TextView 和一个文本字段,我想在编辑 TextView 时移动屏幕。这是我的代码

    override func viewWillAppear(_ animated: Bool) {
super.viewWillAppear(animated)

NotificationCenter.default.addObserver(self, selector: #selector(keyboardWillShow(_:)), name: .UIKeyboardWillShow, object: nil)

// NotificationCenter.default.addObserver(self, selector: #selector(keyboardWillHide(_:)), name: .UIKeyboardWillHide, object: nil)

print("addd observer")

}
override func viewWillDisappear(_ animated: Bool) {
super.viewWillDisappear(animated)
NotificationCenter.default.removeObserver(self, name: .UIKeyboardWillShow, object: nil)
// NotificationCenter.default.removeObserver(self, name: .UIKeyboardWillHide, object: nil)
}

@objc func keyboardWillShow(_ sender: Notification) {
let keyboardSize = (sender.userInfo?[UIKeyboardFrameEndUserInfoKey] as? NSValue)?.cgRectValue
print("keyboardwillshow")
ksize = keyboardSize?.height

}


@objc func textViewDidBeginEditing(_ textView: UITextView) {
print("textviewedit")
boxview.frame.origin.y -= ksize
print("textview")



}
@objc func textViewDidEndEditing(_ textView: UITextView) {

print("textvieweditend")
boxview.frame.origin.y = 0

}

当我只编辑 TextView 时它起作用。但是,如果我在焦点位于文本字段后立即编辑 TextView (没有退出键盘),它就不起作用。我已经检查过 textviewDidBeginEditing() 被调用,但屏幕没有移动。我该如何解决这个问题?欢迎所有建议!

最佳答案

我将self.view.Layoutifneeded()添加到keyboardwillchange()并且它有效!谢谢大家。

关于键盘出现时屏幕快速移动,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51007196/

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