2

I have a UIView ("myUIView") in my ViewController ("mainView") that has a UILabel ("myUILabel") inside, centered vertically with autolayout constraints. I'm animating this UIView to change its height.

Originally, I thought auto layout would keep track of the animation/change and keep the UILabel centered, but instead the UILabel acts as though it is constrained to the top of the UIView.

The UILabel isn't in the animation code itself. Code for reference:

UIView.animateWithDuration(currentAnimationTime, delay: 0.0, options: .CurveEaseOut, animations: { 
self.myUIView.frame = CGRectMake(0, self.mainView.frame.height * 0.2, self.mainView.frame.width, self.mainView.frame.height * 0.6 )
}, completion: nil)

How can I increase the UIView's height and keep my UILabel entered vertically? Will I need to add something that directly affects the UILabel itself, or am I going about the resize itself (CGRectMake) all wrong?

ADRSPR
  • 111
  • 1
  • 3
  • 8

1 Answers1

2

I have had luck by calling layoutIfNeeded on the animated view inside the animation.

It would be preferable to create an outlet for the height constraint of the view and then set the constant of that constraint rather than constructing a CGRect. Change the constant outside the animation as demonstrated in this answer.

Community
  • 1
  • 1
Mundi
  • 79,884
  • 17
  • 117
  • 140
  • Thanks for the lead. I haven't gotten too far with it, I'm afraid. I'm needing to manipulate this UIView quite frequently, and using programmatic constraints is throwing all kinds of errors, and most of the time not working at all. Is this the best way to go for frequent size and position changes? – ADRSPR Mar 28 '15 at 08:43