Swift 将UIView推到键盘上方的动画约束问题

Swift 将UIView推到键盘上方的动画约束问题,swift,uiview,nslayoutconstraint,uiviewanimation,nsnotificationcenter,Swift,Uiview,Nslayoutconstraint,Uiviewanimation,Nsnotificationcenter,我有这个CommentViewController。它嵌入到容器视图中。在这个CommentViewController中,有一个UITableView来显示注释和UIView,其中包含一个UIExtField和一个UIButton。包含这两个对象的UIView通过自动布局固定在安全区域的底部、右侧和左侧,以及UITableView的顶部 现在,当用户点击UITextField时,整个视图(按钮和文本字段在里面)应该被提升到键盘上方。我正在尝试设置视图底部约束的动画,但它不起作用 这是代码: o

我有这个
CommentViewController
。它嵌入到容器视图中。在这个
CommentViewController
中,有一个
UITableView
来显示注释和
UIView
,其中包含一个
UIExtField
和一个
UIButton
。包含这两个对象的
UIView
通过自动布局固定在安全区域的底部、右侧和左侧,以及
UITableView
的顶部

现在,当用户点击
UITextField
时,整个视图(按钮和文本字段在里面)应该被提升到键盘上方。我正在尝试设置视图底部约束的动画,但它不起作用

这是代码:

override func viewDidLoad() {
    super.viewDidLoad()
    NotificationCenter.default.addObserver(self, selector: #selector(self.keyboardWillShow(_:)), name: NSNotification.Name.UIKeyboardWillShow, object: nil)
    NotificationCenter.default.addObserver(self, selector: #selector(self.keyboardWillHide(_:)), name: NSNotification.Name.UIKeyboardWillHide, object: nil)

}

@objc func keyboardWillShow(_ notification: NSNotification) {
    let keyboardFrame = (notification.userInfo?[UIKeyboardFrameEndUserInfoKey] as AnyObject).cgRectValue
    UIView.animate(withDuration: 0.3) {
        self.textFieldViewBottomConstraint.constant = keyboardFrame!.height
    }
}
这是故事板:

override func viewDidLoad() {
    super.viewDidLoad()
    NotificationCenter.default.addObserver(self, selector: #selector(self.keyboardWillShow(_:)), name: NSNotification.Name.UIKeyboardWillShow, object: nil)
    NotificationCenter.default.addObserver(self, selector: #selector(self.keyboardWillHide(_:)), name: NSNotification.Name.UIKeyboardWillHide, object: nil)

}

@objc func keyboardWillShow(_ notification: NSNotification) {
    let keyboardFrame = (notification.userInfo?[UIKeyboardFrameEndUserInfoKey] as AnyObject).cgRectValue
    UIView.animate(withDuration: 0.3) {
        self.textFieldViewBottomConstraint.constant = keyboardFrame!.height
    }
}

这是点击文本字段后的结果。

override func viewDidLoad() {
    super.viewDidLoad()
    NotificationCenter.default.addObserver(self, selector: #selector(self.keyboardWillShow(_:)), name: NSNotification.Name.UIKeyboardWillShow, object: nil)
    NotificationCenter.default.addObserver(self, selector: #selector(self.keyboardWillHide(_:)), name: NSNotification.Name.UIKeyboardWillHide, object: nil)

}

@objc func keyboardWillShow(_ notification: NSNotification) {
    let keyboardFrame = (notification.userInfo?[UIKeyboardFrameEndUserInfoKey] as AnyObject).cgRectValue
    UIView.animate(withDuration: 0.3) {
        self.textFieldViewBottomConstraint.constant = keyboardFrame!.height
    }
}

PS:当我点击
UITextField
时,控制台显示以下消息:

2018-06-04 14:11:52.471848+0300 AppName[91846:8829073][MC]systemgroup.com.apple.configurationprofiles路径为/Users/d/Library/Developer/CoreSimulator/Devices/C89347A2-1598-4F31-BBAC-1F98F970A248/data/Containers/Shared/systemgroup/systemgroup.com.apple.configurationprofiles
2018-06-04 14:11:52.472588+0300托运人[91846:8829073][MC]从私人有效用户设置读取。

更改约束的常量后,需要调用
self.view.layoutifneed()

self.textFieldViewBottomConstraint.constant = keyboardFrame!.height
   UIView.animate(withDuration: 0.3) {
    self.view.layoutIfNeeded()
}
//

编辑:

为底部约束设置常量属性取决于约束中的第一个元素,因此如果约束看起来像这样

这里textfield是第一个

textfield_Bottom=视图_Bottom*乘数+常数。->然后 常数必须是负的,因为Y轴在上升时减小,我们知道 要在显示键盘时使视图上升吗

这里是第一个视图

视图底部=文本字段底部*乘数+常数。->然后 常数必须为正


如果您只需要功能。看看这个,对于[MC]消息,它是不相关的,你可以忽略这些:试试UIKeyboardDidShow你可以更具体一点吗?很遗憾,没有。同样,没有结果。问题可能是该
CommentViewController
实际上在容器视图中吗?另外,当我点击
UITextField
时,控制台会显示以下消息:(查看我更新的答案)你能在函数内部打印任何内容以查看它是否运行吗是的,我还怀疑函数没有被调用,但它确实被调用了。我看到了打印的声明