当键盘出现在我的应用程序中时,按钮部分向上移动(只有按钮的文本移动)

when a keyboard shows up in my app, the button partially moves up (only the text of the button moves)

我的 swift 应用程序中有一个按钮位于屏幕底部。它的约束条件是:

我还附加了插座以将我的按钮与屏幕底部分开:

当我 运行 应用程序时,我看到了我的按钮(我添加了一些背景颜色以便我的示例清晰可见):

现在,奇怪的事情发生了——当键盘显示时,按钮上的文字向上移动,蓝色背景保持原样:

而且按钮的可见部分根本无法点击。

这是我的实现中的某种错误或问题吗?

我的代码非常简单:

@IBOutlet weak var continueUsernameBottomConstraint: NSLayoutConstraint!

override func viewDidAppear(_ animated: Bool) {
    super.viewDidAppear(animated)


    NotificationCenter.default.addObserver(self, selector: #selector(tutorialKeyboardWillAppear), name: .UIKeyboardWillShow, object: nil)

    NotificationCenter.default.addObserver(self, selector: #selector(tutorialKeyboardWillDisappear), name: .UIKeyboardWillHide, object: nil)

}


func tutorialKeyboardWillAppear(notification: NSNotification){
    print("KEYBOARD APPEARS")
    let endFrame = ((notification as NSNotification).userInfo![UIKeyboardFrameEndUserInfoKey] as! NSValue).cgRectValue

    continueUsernameBottomConstraint.constant = view.bounds.height - endFrame.origin.y

    self.view.layoutIfNeeded()
}

func tutorialKeyboardWillDisappear(notification: NSNotification){

    print("KEYBOARD DISAPPEARS")
    let endFrame = ((notification as NSNotification).userInfo![UIKeyboardFrameEndUserInfoKey] as! NSValue).cgRectValue

    continueUsernameBottomConstraint.constant = view.bounds.height - endFrame.origin.y

    self.view.layoutIfNeeded()

}

使用这个

func tutorialKeyboardWillAppear(notification: NSNotification){
    print("KEYBOARD APPEARS")
    let endFrame = ((notification as NSNotification).userInfo![UIKeyboardFrameEndUserInfoKey] as! NSValue).cgRectValue
    continueUsernameBottomConstraint.constant = continueUsernameBottomConstraint.constant + CGFloat(endFrame.height)
    self.view.layoutIfNeeded()
}

func tutorialKeyboardWillDisappear(notification: NSNotification){

    print("KEYBOARD DISAPPEARS")
    let endFrame = ((notification as NSNotification).userInfo![UIKeyboardFrameEndUserInfoKey] as! NSValue).cgRectValue
    continueUsernameBottomConstraint.constant = continueUsernameBottomConstraint.constant -  CGFloat(endFrame.height)
    self.view.layoutIfNeeded()

}