i have a strange problem on a very simple task.
I needs a very small setup to reproduce the problem:
|--------------------------------------|
| Parent View |
| |
||------------------------------------||
|| UILabel ||
||------------------------------------||
| |
| |
||------------------------------------||
|| UIView ||
||------------------------------------||
| |
| |
|--------------------------------------|
In the example above we have a parent view with 2 subviews --> A UILabel and a simple UIView. Im using autolayout to apply the layout shown above:
Constraints for UILabel:
Leading Space to superview = 0
Trailing Space to superview = 0
Fixed Height constraint (e.g. 80pt)
Top Space constraint (e.g. 50pt)
The UIView subview has the same constraint types (the values for height and top space differs).
So what i expect with this setup, is that both subviews will adopt the full width of its parent as we defined that leading and trailing space should be zero. so if change the width of the parent view, the width of the subviews should change too, to stay aligned.
lets say the parent view has a dimension of 200x400 pt. when i build and run my example in portrait everything looks ok. when i rotate to landscape, everything still looks fine. the parent views width and even the subviews width got larger. but now when i rotate back to portrait the uilabels width immediately gets its target width without any animation:
|--------------------------------------|
| Parent View |
| |
| (immediately has target size) |
| |--------------| |
| | UILabel | |
| |--------------| |
| |
| (this subview is still |
| animating its width) |
| |------------------------------| |
|-->| UIView |<--|
| |------------------------------| |
| |
| |
|--------------------------------------|
the parent views width and the uiview subviews width are animated properly. its only the uilabel that has a strange behavior here and i cant find out what causes that issue.
My suggestions are:
If you've subclassed the parent view use the - (void)layoutSubviews
:
- (void)layoutSubviews {
label.frame = CGRectMake(leftInset, labelY, parentView.frame.size.width - leftInset*2, labelHeight);
childView.frame = CGRectMake(leftInset, childViewY, parentView.frame.size.width - leftInset*2, childHeight);
}
if you didn't subclassed the parent view you can use autoresizingMask
:
label.autoresizingMask = UIViewAutoresizingFlexibleWidth;
childView.autoresizingMask = UIViewAutoresizingFlexibleWidth;
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With