当键盘弹出时实现向上移动视图时,修复这些难看的结果

时间:2019-07-30 22:59:31

标签: ios swift keyboard

这是我的代码。

    NotificationCenter.default.addObserver(self, selector: #selector(ViewController.keyboardWillShow), name: NSNotification.Name.UIKeyboardDidShow, object: nil)

    NotificationCenter.default.addObserver(self, selector: #selector(ViewController.keyboardWillHide), name: NSNotification.Name.UIKeyboardWillHide, object: nil)


@objc func keyboardWillShow(notification: NSNotification){
    guard let userInfo = notification.userInfo else {return}
    guard let keyboardSize = userInfo[UIKeyboardFrameEndUserInfoKey] as? NSValue else {return}

    let keyboardFrame = keyboardSize.cgRectValue
    if (self.view.frame.origin.y == 0) {
        self.view.frame.origin.y -= keyboardFrame.height
    }
}

@objc func keyboardWillHide(notification: NSNotification) {
    if (self.view.frame.origin.y != 0) {
        self.view.frame.origin.y = 0
    }
}

enter image description here

enter image description here

有没有一种方法可以使textview而不是整个viewcontroller向上移动?

0 个答案:

没有答案