MMMHUHU
为了使视图的大小由放置在其中的内容确定,那么放置在其中的内容必须与包含视图具有显式关系才能驱动其高度(或宽度)动态。“添加子视图”不会创建您可能假定的关系。您必须选择哪个子视图来驱动容器的高度和/或宽度……最常见的是,您在整个UI的右下角放置了任何UI元素。这是一些代码和内联注释来说明这一点。请注意,这对于使用滚动视图的用户来说可能特别有价值,因为通常围绕一个内容视图进行设计,该内容视图根据您放置的内容动态确定其大小(并将其传达给滚动视图)。祝您好运,希望这对您有所帮助。//// ViewController.m// AutoLayoutDynamicVerticalContainerHeight//#import "ViewController.h"@interface ViewController ()@property (strong, nonatomic) UIView *contentView;@property (strong, nonatomic) UILabel *myLabel;@property (strong, nonatomic) UILabel *myOtherLabel;@end@implementation ViewController- (void)viewDidLoad{ // INVOKE SUPER [super viewDidLoad]; // INIT ALL REQUIRED UI ELEMENTS self.contentView = [[UIView alloc] init]; self.myLabel = [[UILabel alloc] init]; self.myOtherLabel = [[UILabel alloc] init]; NSDictionary *viewsDictionary = NSDictionaryOfVariableBindings(_contentView, _myLabel, _myOtherLabel); // TURN AUTO LAYOUT ON FOR EACH ONE OF THEM self.contentView.translatesAutoresizingMaskIntoConstraints = NO; self.myLabel.translatesAutoresizingMaskIntoConstraints = NO; self.myOtherLabel.translatesAutoresizingMaskIntoConstraints = NO; // ESTABLISH VIEW HIERARCHY [self.view addSubview:self.contentView]; // View adds content view [self.contentView addSubview:self.myLabel]; // Content view adds my label (and all other UI... what's added here drives the container height (and width)) [self.contentView addSubview:self.myOtherLabel]; // LAYOUT // Layout CONTENT VIEW (Pinned to left, top. Note, it expects to get its vertical height (and horizontal width) dynamically based on whatever is placed within). // Note, if you don't want horizontal width to be driven by content, just pin left AND right to superview. [self.view addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"H:|[_contentView]" options:0 metrics:0 views:viewsDictionary]]; // Only pinned to left, no horizontal width yet [self.view addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"V:|[_contentView]" options:0 metrics:0 views:viewsDictionary]]; // Only pinned to top, no vertical height yet /* WHATEVER WE ADD NEXT NEEDS TO EXPLICITLY "PUSH OUT ON" THE CONTAINING CONTENT VIEW SO THAT OUR CONTENT DYNAMICALLY DETERMINES THE SIZE OF THE CONTAINING VIEW */ // ^To me this is what's weird... but okay once you understand... // Layout MY LABEL (Anchor to upper left with default margin, width and height are dynamic based on text, font, etc (i.e. UILabel has an intrinsicContentSize)) [self.view addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"H:|-[_myLabel]" options:0 metrics:0 views:viewsDictionary]]; [self.view addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"V:|-[_myLabel]" options:0 metrics:0 views:viewsDictionary]]; // Layout MY OTHER LABEL (Anchored by vertical space to the sibling label that comes before it) // Note, this is the view that we are choosing to use to drive the height (and width) of our container... // The LAST "|" character is KEY, it's what drives the WIDTH of contentView (red color) [self.view addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"H:|-[_myOtherLabel]-|" options:0 metrics:0 views:viewsDictionary]]; // Again, the LAST "|" character is KEY, it's what drives the HEIGHT of contentView (red color) [self.view addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"V:[_myLabel]-[_myOtherLabel]-|" options:0 metrics:0 views:viewsDictionary]]; // COLOR VIEWS self.view.backgroundColor = [UIColor purpleColor]; self.contentView.backgroundColor = [UIColor redColor]; self.myLabel.backgroundColor = [UIColor orangeColor]; self.myOtherLabel.backgroundColor = [UIColor greenColor]; // CONFIGURE VIEWS // Configure MY LABEL self.myLabel.text = @"HELLO WORLD\nLine 2\nLine 3, yo"; self.myLabel.numberOfLines = 0; // Let it flow // Configure MY OTHER LABEL self.myOtherLabel.text = @"My OTHER label... This\nis the UI element I'm\narbitrarily choosing\nto drive the width and height\nof the container (the red view)"; self.myOtherLabel.numberOfLines = 0; self.myOtherLabel.font = [UIFont systemFontOfSize:21];}@end