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?
Intrinsic content size is information that a view has about how big it should be based on what it displays. For example, a label's intrinsic content size is based on how much text it is displaying. In your case, the image view's intrinsic content size is the size of the image that you selected.
Returns the optimal size of the view based on its current constraints.
translatesAutoresizingMaskIntoConstraints. A Boolean value that determines whether the view's autoresizing mask is translated into Auto Layout constraints.
sizeToFit()Resizes and moves the receiver view so it just encloses its subviews.
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.
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