I have UILabel
and UIButton
inside view which can have different size, and I want label to be hidden completely if it can't fit all the content within current frame using only AutoLayout. So basically I want it to follow it's intrinsic size, and if it shrink, it should shrink straight to zero without any middle width. Desired example:
Instead, I'm having it shrinking and trying to display as much as possible:
Is it possible to do only using AutoLayout? If not, why?
I tried to add zero-width constraint on label and set it's priority to be less than compression resistance of the label, but this does not work. I thought that once Autolayout engine broke intristic size rule, mine zero-width will be followed, but it seems I'm missing something.
EDIT: I would accept to embed label in some UIView
subclass that can check intristic sizes of subviews and do layout in code, but I'm searching for most clean solution in general.
My current solutions is to embed label into custom UIView
and during it's layout I directly check if size got smaller than intristic size:
@implementation AutocollapsibleView
- (void)layoutSubviews {
UIView *subview = self.subviews.firstObject;
if (self.bounds.size.width < subview.intrinsicContentSize.width) {
subview.frame = CGRectZero;
} else {
[super layoutSubviews];
}
}
@end
Fortunately it works with IB_DESIGNABLE
marco so I instantly see result in the Interface Builder