Does a UIView create an intrinsicContentSize?
I create a UIView contentView. I do not give it constraints size:
UIView *contentView = [UIView new];
[contentView setTranslatesAutoresizingMaskIntoConstraints:NO];
contentView.backgroundColor = [UIColor orangeColor];
I create another UIView subview01. I give it constraints size and add it to my contentView:
UIImageView *imageView = [[UIImageView alloc] initWithImage:image];
[imageView setTranslatesAutoresizingMaskIntoConstraints:NO];
imageView.userInteractionEnabled = TRUE;
imageView.backgroundColor = [UIColor clearColor];
[imageView addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"H:[imageView(WIDTH)]"
options:0
metrics:@{@"WIDTH" : [NSNumber numberWithFloat:imageSize.width]}
views:NSDictionaryOfVariableBindings(imageView)]];
[imageView addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"V:[imageView(HEIGHT)]"
options:0
metrics:@{@"HEIGHT" : [NSNumber numberWithFloat:imageSize.height]}
views:NSDictionaryOfVariableBindings(imageView)]];
[contentView addSubview:imageView];
contentView does does not gain any size. I thought the intrinsicContentSize is suppose to calculate the size needed to show all subviews and resize itself? Like how a UILabel will resize to show all of its text?
No, a UIView does not have an intrinsicContentSize. Buttons and labels do, since it’s easy for the system to calculate that size based on the string and or image in them. For a UIView, you generally need 4 constraints to fully describe its position and size.