簡體   English   中英

每次我重新點擊文本字段時,視圖都會發生變化

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

所以我有一個小應用程序,我可以在其中選擇一個圖像,將其放入圖像視圖中,並且我有 2 個文本字段來插入一個有趣的短語

(模因編輯器應用程序)我的問題是,因為當顯示鍵盤時底部文本字段被覆蓋,所以每次為底部 texfield 顯示鍵盤時,我都必須向上移動視圖,我成功地做到了,問題是每次我在文本文件中重新點擊現有文本的開頭或結尾,視圖再次以不良行為向上移動

這是一個小 GIF,顯示了到底發生了什么

到目前為止,這是我的代碼:

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.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM