Padin215
Padin215

Reputation: 7484

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?

Upvotes: 5

Views: 6193

Answers (2)

Padin215
Padin215

Reputation: 7484

While not a perfect solution, I did figure out a way to mimic intrinsicContentSize.

I set the container view's NSLayoutAttributeRight to the right most sub view's NSLayoutAttributeRight and do the same for NSLayoutAttributeBottom to the lowest sub view's NSLayoutAttributeBottom.

[self.view addConstraint:[NSLayoutConstraint constraintWithItem:self
                                                          attribute:NSLayoutAttributeRight
                                                          relatedBy:NSLayoutRelationEqual
                                                             toItem:rightSubView
                                                          attribute:NSLayoutAttributeRight
                                                         multiplier:1 constant:0]];

[self.view addConstraint:[NSLayoutConstraint constraintWithItem:self
                                                          attribute:NSLayoutAttributeBottom
                                                          relatedBy:NSLayoutRelationEqual
                                                             toItem:bottumSubView
                                                          attribute:NSLayoutAttributeBottom
                                                         multiplier:1 constant:0]];

Upvotes: 0

rdelmar
rdelmar

Reputation: 104092

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.

Upvotes: 9

Related Questions