打开键盘时移动视图的问题

时间:2019-09-14 13:04:13

标签: ios swift xcode

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

 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吗?

谢谢!

1 个答案:

答案 0 :(得分:0)

std::list