在UIViewController中,我有几个文本字段和一个按钮在UIViewController的底部。对于按钮,我设置了一个常量为0的底部约束。然后,我从底部约束到UIViewController创建了一个outlet。
当我运行我的代码时,按钮不会向上移动。我在stackoverflow上看到了建议,说我应该添加UIScrollView,但这意味着我必须删除UIViewController上的所有对象,放置UIScrollView,然后再将我的对象放回到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()
}
keyboardWillShow
和keyboardWillHide
的实现,但我已经修复了这个问题,所以它不应该再让东西消失了。 - Ryan Cocuzzo