简体   繁体   English

每次我重新点击文本字段时,视图都会发生变化

[英]View is getting shifted every time I re-tap textfield

so I have a small app where I choose an image, get it into an image view and I have 2 textfield to insert a funny phrase所以我有一个小应用程序,我可以在其中选择一个图像,将其放入图像视图中,并且我有 2 个文本字段来插入一个有趣的短语

(Meme editor app) my problem is since the bottom textfield is covered when the keyboard is shown I had to shift the view upwards every time the keyboard is shown for the bottom texfield and I succeed in doing that, what goes wrong is that every time I re-tap the beginning or the end of an existing text in the text filed the view shifts up again in undesirable behavior (模因编辑器应用程序)我的问题是,因为当显示键盘时底部文本字段被覆盖,所以每次为底部 texfield 显示键盘时,我都必须向上移动视图,我成功地做到了,问题是每次我在文本文件中重新点击现有文本的开头或结尾,视图再次以不良行为向上移动

here is a small GIF that shows what happens exactly这是一个小 GIF,显示了到底发生了什么

here is my code so far:到目前为止,这是我的代码:

Function to get Kyboard height: 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 to shift the view up in the condition that bottom textfield is what the user taps Function 在底部文本字段是用户点击的情况下向上移动视图

@objc func keyboardWillShow(_ notification:Notification) {
        if bottomTextField.isFirstResponder{
            view.frame.origin.y -= getKeyboardHeight(notification)
        }
    }

Function to return the view to its normal position when the user finishes editing the bottom text field Function 当用户完成编辑底部文本字段时,将视图返回到其正常 position

@objc func keyboardWillHide(_ notification:Notification) {
    view.frame.origin.y = 0
}

Functions to add and remove observers of keyboard notifications添加和删除键盘通知观察者的功能

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)
    }

and where I call them我叫他们的地方

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()
    
}

if you could be kind to provide a simple explanation for your solution I would appreciate it如果您愿意为您的解决方案提供一个简单的解释,我将不胜感激

Every time you change the cursor location, UIResponder.keyboardWillShowNotification notification triggered, thats why every time you tap the textfield it moves up one keyboard height more.每次更改 cursor 位置时,都会触发UIResponder.keyboardWillShowNotification通知,这就是为什么每次点击文本字段时它都会向上移动一个键盘高度。

You can use UIResponder.keyboardDidShowNotification notification instead of UIResponder.keyboardWillShowNotification .您可以使用UIResponder.keyboardDidShowNotification通知而不是UIResponder.keyboardWillShowNotification This one is not triggered when cursor location changes.当 cursor 位置发生变化时,不会触发这个。

This is what I do while managing keyboard.这就是我在管理键盘时所做的。 It causes no problems.它不会引起任何问题。

Declare in your UIViewController class在您的 UIViewController class 中声明

private let notificationCenter = NotificationCenter.default

then in然后在

 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)
    }

This is the adjust for keyboard function这是键盘 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 here -在这里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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM