优雅地将 UIView 移动到键盘的 inputAccessoryView
Elegantly move a UIView to a keyboard's inputAccessoryView
有没有一种优雅的方法可以将位于其父视图底部的 UIView
(即 UIButton
)无缝移动到键盘的 inputAccessoryView
?
我设想键盘基本上是拿起按钮并在它滑入时向上拖动它。请看下面的模型。显然,我也希望它能以相反的方式工作。
我知道如何分别做,但我不知道如何结合起来。有人以前做过吗?
谢谢!
更新:我忘了说我主要是在 UIScrollViews
的背景下工作。虽然下面安德烈的回答适用于普通视图,但在滚动视图中使用时会中断。
在这种情况下,您可以忽略使用 inputaccessoryview,而是调整登录按钮对键盘通知的底部限制。
class ViewController: UIViewController {
@IBOutlet weak var signInButtonBottomConstraint: NSLayoutConstraint!
override func viewDidLoad() {
super.viewDidLoad()
NSNotificationCenter.defaultCenter().addObserver(self, selector: Selector("keyboardWillShow:"), name: UIKeyboardWillShowNotification, object: nil)
NSNotificationCenter.defaultCenter().addObserver(self, selector: Selector("keyboardWillHide:"), name: UIKeyboardWillHideNotification, object: nil)
}
func keyboardWillShow(notification: NSNotification) {
let userInfo = notification.userInfo!
let animationDuration = userInfo[UIKeyboardAnimationDurationUserInfoKey] as! Double
let keyboardEndFrame = (userInfo[UIKeyboardFrameEndUserInfoKey] as! NSValue).CGRectValue()
signInButtonBottomConstraint.constant = keyboardEndFrame.height
UIView.animateWithDuration(animationDuration) { () -> Void in
self.view.layoutIfNeeded()
}
}
func keyboardWillHide(notification: NSNotification) {
let userInfo = notification.userInfo!
let animationDuration = userInfo[UIKeyboardAnimationDurationUserInfoKey] as! Double
signInButtonBottomConstraint.constant = 0.0
UIView.animateWithDuration(animationDuration) { () -> Void in
self.view.layoutIfNeeded()
}
}
}
有没有一种优雅的方法可以将位于其父视图底部的 UIView
(即 UIButton
)无缝移动到键盘的 inputAccessoryView
?
我设想键盘基本上是拿起按钮并在它滑入时向上拖动它。请看下面的模型。显然,我也希望它能以相反的方式工作。
我知道如何分别做,但我不知道如何结合起来。有人以前做过吗?
谢谢!
更新:我忘了说我主要是在 UIScrollViews
的背景下工作。虽然下面安德烈的回答适用于普通视图,但在滚动视图中使用时会中断。
在这种情况下,您可以忽略使用 inputaccessoryview,而是调整登录按钮对键盘通知的底部限制。
class ViewController: UIViewController {
@IBOutlet weak var signInButtonBottomConstraint: NSLayoutConstraint!
override func viewDidLoad() {
super.viewDidLoad()
NSNotificationCenter.defaultCenter().addObserver(self, selector: Selector("keyboardWillShow:"), name: UIKeyboardWillShowNotification, object: nil)
NSNotificationCenter.defaultCenter().addObserver(self, selector: Selector("keyboardWillHide:"), name: UIKeyboardWillHideNotification, object: nil)
}
func keyboardWillShow(notification: NSNotification) {
let userInfo = notification.userInfo!
let animationDuration = userInfo[UIKeyboardAnimationDurationUserInfoKey] as! Double
let keyboardEndFrame = (userInfo[UIKeyboardFrameEndUserInfoKey] as! NSValue).CGRectValue()
signInButtonBottomConstraint.constant = keyboardEndFrame.height
UIView.animateWithDuration(animationDuration) { () -> Void in
self.view.layoutIfNeeded()
}
}
func keyboardWillHide(notification: NSNotification) {
let userInfo = notification.userInfo!
let animationDuration = userInfo[UIKeyboardAnimationDurationUserInfoKey] as! Double
signInButtonBottomConstraint.constant = 0.0
UIView.animateWithDuration(animationDuration) { () -> Void in
self.view.layoutIfNeeded()
}
}
}