gpt4 book ai didi

ios - 通过键盘向上滚动后,TextView 没有移回原始位置

转载 作者:搜寻专家 更新时间:2023-11-01 07:28:26 25 4
gpt4 key购买 nike

编辑:添加 viewDidLoad 代码

所以我做了一些研究来学习如何移动我的 TextView 之一,这样当键盘弹出时,它会向上 ScrollView ,这样键盘就不会覆盖 TextView 。我似乎能够完成这项工作,但现在我的问题是,在关闭键盘后, View 仍保持原样。它不会“快速”回到原来的位置,然后键盘弹出并将 TextView 滚动到一边。我在我的代码中遗漏了什么吗?或者我需要添加一些东西吗?

override func viewDidLoad() {
super.viewDidLoad()
self.questionInput.delegate = self
self.answerInput.delegate = self
let tap: UITapGestureRecognizer = UITapGestureRecognizer(target:self, action: "DismissKeyboard")
view.addGestureRecognizer(tap)
NSNotificationCenter.defaultCenter().addObserver(self, selector: "keyboardDidShow:", name: UIKeyboardDidShowNotification, object: nil)
NSNotificationCenter.defaultCenter().addObserver(self, selector: "keyboardWillHide:", name: UIKeyboardWillHideNotification, object: nil)
}


var activeView: UITextView?


func textViewDidEndEditing(textView: UITextView) {

self.activeView = nil
}

func textViewDidBeginEditing(textView: UITextView) {

self.activeView = textView
}

func keyboardDidShow(notification: NSNotification) {
if let activeView = self.answerInput, keyboardSize = (notification.userInfo?[UIKeyboardFrameBeginUserInfoKey] as? NSValue)?.CGRectValue() {
let contentInsets = UIEdgeInsets(top: 0.0, left: 0.0, bottom: keyboardSize.height, right: 0.0)
self.scrollView.contentInset = contentInsets
self.scrollView.scrollIndicatorInsets = contentInsets
var aRect = self.view.frame
aRect.size.height -= keyboardSize.size.height
if (!CGRectContainsPoint(aRect, activeView.frame.origin)) {
self.scrollView.scrollRectToVisible(activeView.frame, animated: true)
}
}
}

func keyboardWillBeHidden(notification: NSNotification) {
let contentInsets = UIEdgeInsetsZero
self.scrollView.contentInset = contentInsets
self.scrollView.scrollIndicatorInsets = contentInsets
}

我的第二个问题是我读过关于需要注销键盘通知的内容,这是准确的引述..

“当您离开 View Controller 时,不要忘记取消注册这些事件。”

这到底是什么意思? segueing时是否需要添加代码?

感谢任何帮助,因为我对这两个问题都很困惑。谢谢。

最佳答案

如果您处于开发的初始阶段,您可以使用这个库,它将处理整个项目中的键盘处理

https://github.com/hackiftekhar/IQKeyboardManager

关于你的第二个问题,这里是你需要实现的示例代码

override func viewDidLoad() {
super.viewDidLoad()
NSNotificationCenter.defaultCenter().addObserver(self, selector: Selector("keyboardWillShow:"), name:UIKeyboardWillShowNotification, object: nil);
NSNotificationCenter.defaultCenter().addObserver(self, selector: Selector("keyboardWillHide:"), name:UIKeyboardWillHideNotification, object: nil);
}

deinit {
NSNotificationCenter.defaultCenter().removeObserver(self);
}

如果你使用自动布局,你需要实现它

func keyboardWasShown(notification: NSNotification) {
var info = notification.userInfo!
var keyboardFrame: CGRect = (info[UIKeyboardFrameEndUserInfoKey] as! NSValue).CGRectValue()

UIView.animateWithDuration(0.1, animations: { () -> Void in
self.bottomConstraint.constant = keyboardFrame.size.height + 20
})
}

关于ios - 通过键盘向上滚动后,TextView 没有移回原始位置,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34348938/

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