Reputation: 23
I noticed a surprising behavior: When I change the frame of a UIView
after receiving the UIKeyboardWillShowNotification
or UIKeyboardWillHideNotification
, the change of the frame is animated. It seems like this animation uses the same duration and easing curve as the keyboard. In this project, I don't use Autolayout, I'm laying views out programmatically by setting their frames.
Can someone explain to me what is going on here?
The interesting parts of the UIViewController's viewDidLoad()
:
NSNotificationCenter.defaultCenter().addObserver(self, selector: #selector(keyboardWillShow), name: UIKeyboardWillShowNotification, object: nil)
NSNotificationCenter.defaultCenter().addObserver(self, selector: #selector(keyboardWillDisappear), name: UIKeyboardWillHideNotification, object: nil)
someView.frame = CGRect(origin: CGPoint(x: 0, y: view.bounds.height - 10), size: CGSize(width: 10, height: 10))
someView.backgroundColor = UIColor.redColor()
view.addSubview(someView)
The callbacks:
func keyboardWillShow(notification: NSNotification) {
someView.frame.origin = CGPoint(x: 0, y: 0)
}
func keyboardWillDisappear(notification: NSNotification) {
someView.frame.origin = CGPoint(x: 0, y: view.bounds.size.height - someView.bounds.size.height)
}
UIKeyboardDid...
notifications UIView.commitAnimations()
before setting my UIViews Frame, but this
seems hacky.UIKeyboardWill...
notification?Upvotes: 2
Views: 379
Reputation: 1410
The answers:
didShow
and didHide
notifications, since they are meant to contain code to be executed after the animation have commited.Hope I helped.
Upvotes: 1