[英]View is getting shifted every time I re-tap textfield
所以我有一个小应用程序,我可以在其中选择一个图像,将其放入图像视图中,并且我有 2 个文本字段来插入一个有趣的短语
(模因编辑器应用程序)我的问题是,因为当显示键盘时底部文本字段被覆盖,所以每次为底部 texfield 显示键盘时,我都必须向上移动视图,我成功地做到了,问题是每次我在文本文件中重新点击现有文本的开头或结尾,视图再次以不良行为向上移动
到目前为止,这是我的代码:
Function 获得 Kyboard 高度:
func getKeyboardHeight(_ notification:Notification) -> CGFloat {
let userInfo = notification.userInfo
let keyboardSize = userInfo![UIResponder.keyboardFrameEndUserInfoKey] as! NSValue // of CGRect
return keyboardSize.cgRectValue.height
}
Function 在底部文本字段是用户点击的情况下向上移动视图
@objc func keyboardWillShow(_ notification:Notification) {
if bottomTextField.isFirstResponder{
view.frame.origin.y -= getKeyboardHeight(notification)
}
}
Function 当用户完成编辑底部文本字段时,将视图返回到其正常 position
@objc func keyboardWillHide(_ notification:Notification) {
view.frame.origin.y = 0
}
添加和删除键盘通知观察者的功能
func subscribeToKeyboardNotifications() {
NotificationCenter.default.addObserver(self, selector: #selector(keyboardWillShow(_:)), name: UIResponder.keyboardWillShowNotification, object: nil)
}
func subscribekeyboardWillHide() {
NotificationCenter.default.addObserver(self, selector: #selector(keyboardWillHide(_:)), name: UIResponder.keyboardWillHideNotification, object: nil)
}
func unsubscribeFromKeyboardNotifications() {
NotificationCenter.default.removeObserver(self, name: UIResponder.keyboardWillShowNotification, object: nil)
}
func unsubscribekeyboardWillHide() {
NotificationCenter.default.removeObserver(self, name: UIResponder.keyboardWillHideNotification, object: nil)
}
我叫他们的地方
override func viewWillAppear(_ animated: Bool) {
super .viewWillAppear(true)
cameraButton.isEnabled = UIImagePickerController.isSourceTypeAvailable(.camera)
subscribeToKeyboardNotifications()
subscribekeyboardWillHide()
}
override func viewWillDisappear(_ animated: Bool) {
super.viewWillDisappear(animated)
unsubscribeFromKeyboardNotifications()
unsubscribekeyboardWillHide()
}
如果您愿意为您的解决方案提供一个简单的解释,我将不胜感激
每次更改 cursor 位置时,都会触发UIResponder.keyboardWillShowNotification
通知,这就是为什么每次点击文本字段时它都会向上移动一个键盘高度。
您可以使用UIResponder.keyboardDidShowNotification
通知而不是UIResponder.keyboardWillShowNotification
。 当 cursor 位置发生变化时,不会触发这个。
这就是我在管理键盘时所做的。 它不会引起任何问题。
在您的 UIViewController class 中声明
private let notificationCenter = NotificationCenter.default
然后在
override func viewDidLoad() {
super.viewDidLoad()
notificationCenter.addObserver(self, selector: #selector(adjustForKeyboard), name: UIResponder.keyboardWillHideNotification, object: nil)
notificationCenter.addObserver(self, selector: #selector(adjustForKeyboard), name: UIResponder.keyboardWillChangeFrameNotification, object: nil)
}
这是键盘 function 的调整
@objc private func adjustForKeyboard(notification: Notification) {
guard let userInfo = notification.userInfo else { return }
guard let keyboardScreenEndFrame = (userInfo[UIResponder.keyboardFrameEndUserInfoKey] as? NSValue)?.cgRectValue else { return }
let keyboardViewEndFrame = view.convert(keyboardScreenEndFrame, from: view.window)
if notification.name == UIResponder.keyboardWillHideNotification {
scrollView.contentInset = UIEdgeInsets.zero
} else {
scrollView.contentInset = UIEdgeInsets(top: 0, left: 0, bottom: keyboardViewEndFrame.height, right: 0)
}
scrollView.scrollIndicatorInsets = scrollView.contentInset
}
在这里Deinit -
override func viewDidDisappear(_ animated: Bool) {
super.viewDidDisappear(true)
notificationCenter.removeObserver(self, name: UIResponder.keyboardWillHideNotification, object: nil)
notificationCenter.removeObserver(self, name: UIResponder.keyboardWillChangeFrameNotification, object: nil)
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.