【发布时间】:2017-01-08 06:47:33
【问题描述】:
在 UIViewController 中,我有几个文本字段,并且按钮位于 UIViewController 的底部。 对于按钮,我设置了一个常数为 0 的底部约束。 然后我做了一个从底部约束到 UIViewController 的出口。
当我运行我的代码时,按钮不会向上移动。我在 stackoverflow 上看到了我应该添加 UIScrollView 的建议,但这意味着,我必须删除 UIViewController 上的所有对象,放置 UIScrollView,然后再次将我的对象放在 UIVIewController 上。
@IBOutlet weak var bottomConstraint: NSLayoutConstraint!
// When tapping outside of the keyboard, close the keyboard down
override func touchesBegan(touches: Set<UITouch>, withEvent event: UIEvent?) {
self.view.endEditing(true)
}
// Stop Editing on Return Key Tap. textField parameter refers to any textfield within the view
func textFieldShouldReturn(textField: UITextField) -> Bool {
textField.resignFirstResponder()
return true
}
// When keyboard is about to show assign the height of the keyboard to bottomConstraint.constant of our button so that it will move up
func keyboardWillShow(notification: NSNotification) {
if let userInfo = notification.userInfo {
if let keyboardSize: CGRect = (userInfo[UIKeyboardFrameBeginUserInfoKey] as? NSValue)?.CGRectValue() {
bottomConstraint.constant = keyboardSize.size.height
view.setNeedsLayout()
}
}
}
// When keyboard is hidden, move the button to the bottom of the view
func keyboardWillHide(notification: NSNotification) {
bottomConstraint.constant = 0.0
view.setNeedsLayout()
}
【问题讨论】:
标签: swift