首页 > 解决方案 > 键盘打开时移动视图的问题

问题描述

当键盘打开时,我在移动文本输入和按钮时遇到问题。我正在使用以下代码

 override func viewDidLoad() {
        super.viewDidLoad()
        NotificationCenter.default.addObserver(self, selector: #selector(keyboardWillChange(notification:)), name: UIResponder.keyboardWillShowNotification, object: nil)

         NotificationCenter.default.addObserver(self, selector: #selector(keyboardWillChange(notification:)), name: UIResponder.keyboardWillChangeFrameNotification, object: nil)
        NotificationCenter.default.addObserver(self, selector: #selector(keyboardWillChange(notification:)), name: UIResponder.keyboardWillHideNotification, object: nil)
}

 deinit {
        NotificationCenter.default.removeObserver(self, name: UIResponder.keyboardWillShowNotification, object: nil)

        NotificationCenter.default.removeObserver(self, name: UIResponder.keyboardWillChangeFrameNotification, object: nil)
        NotificationCenter.default.removeObserver(self, name: UIResponder.keyboardWillHideNotification, object: nil)
    }


    @objc func keyboardWillChange(notification: NSNotification) {

        guard let keyboardSize = (notification.userInfo?[UIResponder.keyboardFrameEndUserInfoKey] as? NSValue)?.cgRectValue else {
            return
        }

        if notification.name == UIResponder.keyboardWillShowNotification || notification.name == UIResponder.keyboardWillChangeFrameNotification {
            view.frame.origin.y = -keyboardSize.height
        } else {
            view.frame.origin.y = 0
        }

    }

当我开始在文本字段中输入时出现问题,这里是之前的图像:https ://imgur.com/a/cbQbJzW和之后:https ://imgur.com/a/f1Nakrs

我很抱歉文件的语言没有说任何空间。

我想知道为什么会发生这种情况,是否可能是因为我在灰色文本字段中使用 CocoaPods - YoshikoTextField ?

谢谢!

标签: iosswiftxcode

解决方案



import UIKit

class ViewController: UIViewController, UITextFieldDelegate {

    @IBOutlet weak var backgroundScrollView: UIScrollView!

    override func viewDidLoad() {
        super.viewDidLoad()

        NotificationCenter.default.addObserver(self, selector: #selector(ViewController.keyboardWillShow), name: UIResponder.keyboardWillShowNotification, object: nil)

        NotificationCenter.default.addObserver(self, selector: #selector(ViewController.keyboardWillHide), name: UIResponder.keyboardWillHideNotification, object: nil)

        // Do any additional setup after loading the view, typically from a nib.
    }

    @objc func keyboardWillShow(notification:NSNotification) {
        adjustingHeight(true, notification: notification)
    }

    @objc func keyboardWillHide(notification:NSNotification) {
        adjustingHeight(false, notification: notification)
    }

    func adjustingHeight(_ isShow:Bool, notification:NSNotification) {

        let userInfo = notification.userInfo!

        let keyboardFrame = (userInfo[UIResponder.keyboardFrameBeginUserInfoKey] as! NSValue).cgRectValue

        let changeInHeight = (keyboardFrame.height + 20) * (isShow ? 1 : -1)

        backgroundScrollView.contentInset.bottom += changeInHeight

        backgroundScrollView.scrollIndicatorInsets.bottom += changeInHeight

    }

    func textFieldShouldReturn(_ textField: UITextField) -> Bool {
        self.view.endEditing(true)
        return true
    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }

}

推荐阅读