Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

UIView and intrinsicContentSize

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?

like image 350
Padin215 Avatar asked Jan 25 '13 18:01

Padin215


People also ask

What is Intrinsiccontentsize?

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.

What is Systemlayoutsizefitting?

Returns the optimal size of the view based on its current constraints.

What is translatesAutoresizingMaskIntoConstraints?

translatesAutoresizingMaskIntoConstraints. A Boolean value that determines whether the view's autoresizing mask is translated into Auto Layout constraints.

What is sizeToFit?

sizeToFit()Resizes and moves the receiver view so it just encloses its subviews.


1 Answers

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.

like image 65
rdelmar Avatar answered Oct 20 '22 01:10

rdelmar