Reputation: 8661
I am using self.navigationController?.setNavigationBarHidden(true, animated: true)
I wonder where I can see how that animation is built in code? I want to mimic its behavior with another custom UIView
Upvotes: 0
Views: 134
Reputation: 37053
The animation is just a slide up, so an animation is created that changes the y-origin of the view being animated.
If using auto layout, you should have a top constraint that specifies that the view's top y position is equal to the superview's top y position. You can reference these NSLayoutConstraint
s using @IBOutlet
like you can with other storyboard elements, so in your view controller:
class MyViewController {
@IBOutlet var viewToAnimate: UIView!
@IBOutlet var topConstraint: NSLayoutConstraint!
}
You will notice that NSLayoutConstraint
has a constant
property, which just specifies a value to add to the second constraint attribute when calculating the resulting layout frames using that constraint. So in the case of the topConstraint
, you want to add -viewToAnimate.bounds.height
*, so that the bottom of the view sits just out of sight at the top of the superview.
You animate changes using the UIView
class method animateWithDuration(_:animations:)
- any animatable UIView
properties that are changed inside the animation
closure will be animated over the specified duration
. But when you change your constraint's constant
property, the view properties aren't changed until another layout pass is performed on the view. You can invalidate the layout, and let the layout happen in the next pass implicitly with view.setNeedsLayout()
, but we need the layout pass to happen inside the view's animation block for the animation to work. So instead, you can use layoutIfNeeded()
to force the subviews to layout immediately.
Put together, your animate method might look something like this:
class MyViewController {
// ...
func animateViewUp() {
UIView.animateWithDuration(0.3) {
self.topConstraint.constant = -self.viewToAnimate.bounds.height
self.view.layoutIfNeeded()
}
}
}
In reality, you would likely specify a method that allows you to toggle between a shown/hidden state, but the above code should get you started.
*N.B. Check which way around the constraint is specified! If the constraint is specified in terms of your superview, then a negative constant
will move the subview downwards!
Upvotes: 1
Reputation: 131511
Short answer: You can't. The source for Apple's frameworks is not provided. However, most of their animations are built on top of the same Core Animation framework that us third party developers use.
What is the effect this creates? I'm not sure I've ever animated away the navigation bar on a view controller that's in place. Can you post a GIF of the animation? I can probably give you an idea of how it's done if I know what it looks like.
Upvotes: 1