I have a view with ≤ constant
height constraint and a fixed width constraint. Inside it, I have a text view with leading/trailing/top/bottom constraints to that outer view, effectively being "docked" inside that view (just with a few point margins). I have my text view's vertical content compression resistance priority set to 750, and it resizes itself depending on the text content. It grows when I go to second line, shrinks when I delete a line etc. However, I want that grow/shrink to be animated. I've added this code both to my text view's and my outer view's setFrame:
code (they are both custom subclasses):
-(void)setFrame:(CGRect)frame{
[super setFrame:frame];
[UIView animateWithDuration:0.2 animations:^{
[self layoutIfNeeded];
} completion:nil];
}
It doesn't work, it's still not animated. Then, I've subclasses NSLayoutConstraint
, and overridden its setConstant:
method with the same code:
-(void)setConstant:(CGFloat)constant{
[super setConstant:constant];
if(self.shouldAnimate){
[UIView animateWithDuration:0.2 animations:^{
[self.firstItem layoutIfNeeded];
} completion:nil];
}
}
It doesn't work too. I've put a breakpoint inside those methods, and they are not called at all. Apparently, layout change happens at a lower level. How can I animate the growth and shrinking of my view? One option is to disable content compression resistance priority, calculate the height of my text view on text change, then set the height attribute manually, and to animate there. But I don't want to go with that if there is a cleaner solution that involves dynamic/automatic resizing using content compression resistance. Is there a solution?