Reputation: 5525
Background:
I have a normal UIView
in Storyboard (called statusView
) which has a height of 30
, and four constraints pinned to the leading, trailing, top space to the superview, and bottom space to the view below it.
Problem:
My goal is to alter the height of statusView
, including animating the frame changes when the user performs an action. As such, statusView
could be as short as 0
or as tall as 100
.
My expectation is that, given statusView
is constrained to the superview and its nearest neighbor below, it should automatically "push" the views below when I alter its height.
Just to test, in viewDidAppear
, I call the following:
self.statusView.frame = CGRect(x: 0, y: 0, width: self.view.frame.size.width, height: 100.0)
self.statusView.setNeedsDisplay()
Issue: However, the height of 30 remains for statusView
, despite me setting it to 0
.
Edit: posted Storyboard
settings
This is the setting for statusView
:
Upvotes: 0
Views: 829
Reputation: 23634
If you are using autolayout and want to set an explicit height for a view in your hierarchy, you should use autolayout to do it. I am assuming there is a height constraint set up in interface builder.
Create an IBOutlet
for the constraint and link it up in interface builder.
@IBOutlet weak var statusViewHeightConstraint: NSLayoutConstraint!
Then, assuming this is just a size constraint with a constant for the height, just change it in your code:
statusViewHeightConstraint.constant = 100
You can animate this change by wrapping a layout call in an animation block right after modifying your constraint(s). Something like this:
UIView.animateKeyframes(withDuration: 0.2, delay: 0.0, options: [.beginFromCurrentState, .calculationModeCubic], animations: {
self.view.layoutIfNeeded()
}, completion: nil)
Upvotes: 3