简体   繁体   English

UIView和intrinsicContentSize

[英]UIView and intrinsicContentSize

Does a UIView create an intrinsicContentSize ? UIView是否创建了intrinsicContentSize

I create a UIView contentView. 我创建了一个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. 我创建了另一个UIView subview01。 I give it constraints size and add it to my contentView: 我给它约束大小并将其添加到我的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. contentView确实没有任何大小。 I thought the intrinsicContentSize is suppose to calculate the size needed to show all subviews and resize itself? 我认为intrinsicContentSize是计算显示所有子视图并调整自身大小所需的大小? Like how a UILabel will resize to show all of its text? 就像UILabel如何调整大小以显示其所有文本一样?

No, a UIView does not have an intrinsicContentSize. 不,UIView没有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. 对于UIView,通常需要4个约束来完全描述其位置和大小。

While not a perfect solution, I did figure out a way to mimic intrinsicContentSize . 虽然不是一个完美的解决方案,但我确实想出了一种模仿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 . 我将容器视图的NSLayoutAttributeRight设置为最右侧的子视图的NSLayoutAttributeRightNSLayoutAttributeRight执行相同的NSLayoutAttributeBottom以使用最低子视图的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]];

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM