Swift 键盘动画上下移动
Swift Keyboard animation moving up and down
单击文本字段时,我的应用会向下移动到白色 space,然后使用键盘返回。我如何让它停止这样做?我只想在激活键盘时让屏幕向上移动一点。
这个问题不同于其他 Swift 键盘问题,因为它使用 Deitel I0S8 for programmers 一书中的代码。解决这个问题可能会帮助其他拥有这本书的人。
谢谢,
override func viewWillAppear(animated: Bool) {
super.viewWillAppear(animated)
NSNotificationCenter.defaultCenter().addObserver(self,
selector: "keyboardWillShow:",
name: UIKeyboardWillShowNotification,
object: nil)
NSNotificationCenter.defaultCenter().addObserver(self,
selector: "keyboardWillHide:",
name: UIKeyboardWillHideNotification,
object: nil) // listen for keyboard show/hide notifications
}
override func viewWillDisappear(animated: Bool) {
super.viewWillDisappear(animated)
NSNotificationCenter.defaultCenter().removeObserver(self,
name: UIKeyboardWillShowNotification, object: nil)
NSNotificationCenter.defaultCenter().removeObserver(self,
name: UIKeyboardWillHideNotification, object: nil) // unregister for keyboard show/hide notifications
}
// called when app receives UIKeyboardWillShowNotification
func keyboardWillShow(notification: NSNotification) {
let userInfo = notification.userInfo!
let frame = userInfo[UIKeyboardFrameEndUserInfoKey] as! NSValue!
let size = frame.CGRectValue().size // keyboard's size
// get duration of keyboard's slide-in animation
let animationTime = userInfo[UIKeyboardAnimationDurationUserInfoKey]!.doubleValue
// scroll self.tableView so selected UITextField above keyboard
UIView.animateWithDuration(animationTime) {
var insets = self.tableView.contentInset
insets.bottom = size.height
self.tableView.contentInset = insets
self.tableView.scrollIndicatorInsets = insets
}
} // called when app receives UIKeyboardWillHideNotification
func keyboardWillHide(notification: NSNotification) {
var insets = self.tableView.contentInset
insets.bottom = 2
self.tableView.contentInset = insets
self.tableView.scrollIndicatorInsets = insets
}
// hide keyboard if user touches Return key
func textFieldShouldReturn(textField: UITextField) -> Bool {
textField.resignFirstResponder()
return true
}
如果您调整 contentInset
属性,您还应该调整 contentOffset
属性 并调整它以实现您想要的行为。但是我建议不要使用插图,而是鼓励您选择两条路之一:
1) 如果使用自动布局:
如果您正在使用自动版式并且将约束连接为 IBOutlet
,则 willShow
将约束常量调整为您需要的任何值。
self.constraint.constant = 2
self.view.layoutIfNeeded()
这将在没有动画的情况下调整它并设置动画:
self.constraint.constant = 2
UIView.animateWithDuration(duration, animations: {
self.view.layoutIfNeeded()
})
然后当 willHide
被调用时,只需将约束常量设置回 0(或任何它的 default/original 值并设置动画)。
2) 如果不使用自动版式:
不要调整约束,而是根据需要调整要移动和设置动画的视图的框架位置。
单击文本字段时,我的应用会向下移动到白色 space,然后使用键盘返回。我如何让它停止这样做?我只想在激活键盘时让屏幕向上移动一点。 这个问题不同于其他 Swift 键盘问题,因为它使用 Deitel I0S8 for programmers 一书中的代码。解决这个问题可能会帮助其他拥有这本书的人。 谢谢,
override func viewWillAppear(animated: Bool) {
super.viewWillAppear(animated)
NSNotificationCenter.defaultCenter().addObserver(self,
selector: "keyboardWillShow:",
name: UIKeyboardWillShowNotification,
object: nil)
NSNotificationCenter.defaultCenter().addObserver(self,
selector: "keyboardWillHide:",
name: UIKeyboardWillHideNotification,
object: nil) // listen for keyboard show/hide notifications
}
override func viewWillDisappear(animated: Bool) {
super.viewWillDisappear(animated)
NSNotificationCenter.defaultCenter().removeObserver(self,
name: UIKeyboardWillShowNotification, object: nil)
NSNotificationCenter.defaultCenter().removeObserver(self,
name: UIKeyboardWillHideNotification, object: nil) // unregister for keyboard show/hide notifications
}
// called when app receives UIKeyboardWillShowNotification
func keyboardWillShow(notification: NSNotification) {
let userInfo = notification.userInfo!
let frame = userInfo[UIKeyboardFrameEndUserInfoKey] as! NSValue!
let size = frame.CGRectValue().size // keyboard's size
// get duration of keyboard's slide-in animation
let animationTime = userInfo[UIKeyboardAnimationDurationUserInfoKey]!.doubleValue
// scroll self.tableView so selected UITextField above keyboard
UIView.animateWithDuration(animationTime) {
var insets = self.tableView.contentInset
insets.bottom = size.height
self.tableView.contentInset = insets
self.tableView.scrollIndicatorInsets = insets
}
} // called when app receives UIKeyboardWillHideNotification
func keyboardWillHide(notification: NSNotification) {
var insets = self.tableView.contentInset
insets.bottom = 2
self.tableView.contentInset = insets
self.tableView.scrollIndicatorInsets = insets
}
// hide keyboard if user touches Return key
func textFieldShouldReturn(textField: UITextField) -> Bool {
textField.resignFirstResponder()
return true
}
如果您调整 contentInset
属性,您还应该调整 contentOffset
属性 并调整它以实现您想要的行为。但是我建议不要使用插图,而是鼓励您选择两条路之一:
1) 如果使用自动布局:
如果您正在使用自动版式并且将约束连接为 IBOutlet
,则 willShow
将约束常量调整为您需要的任何值。
self.constraint.constant = 2
self.view.layoutIfNeeded()
这将在没有动画的情况下调整它并设置动画:
self.constraint.constant = 2
UIView.animateWithDuration(duration, animations: {
self.view.layoutIfNeeded()
})
然后当 willHide
被调用时,只需将约束常量设置回 0(或任何它的 default/original 值并设置动画)。
2) 如果不使用自动版式:
不要调整约束,而是根据需要调整要移动和设置动画的视图的框架位置。