为什么这段代码不能作为垂直对齐的UILabels的容器?怎么了?
self.daysContainer = [[UIView alloc]init];
[self.daysContainer setTranslatesAutoresizingMaskIntoConstraints:NO];
UILabel *dayview1 = [[UILabel alloc]init];
UILabel *dayview2 = [[UILabel alloc]init];
[dayview1 setTranslatesAutoresizingMaskIntoConstraints:NO];
[dayview2 setTranslatesAutoresizingMaskIntoConstraints:NO];
[self.daysContainer addSubview:dayview1];
[self.daysContainer addSubview:dayview2];
dayview1.text = @"DayView 1";
dayview2.text = @"DayView 2";
dayview1.backgroundColor = [UIColor redColor];
NSDictionary *elementsDict = NSDictionaryOfVariableBindings(dayview1, dayview2);
[self.daysContainer addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"V:|-[dayview1]-[dayview2]-|" options:NSLayoutFormatDirectionLeadingToTrailing metrics:nil views:elementsDict]];
[self.daysContainer addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"H:|-[dayview1]-|" options:NSLayoutFormatDirectionLeadingToTrailing metrics:nil views:elementsDict]];
[self.daysContainer addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"H:|-[dayview2]-|" options:NSLayoutFormatDirectionLeadingToTrailing metrics:nil views:elementsDict]];
答案 0 :(得分:0)
看来问题是垂直约束的唯一来源
[self.daysContainer addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"V:|-[dayview1]-[dayview2]-|" options:NSLayoutFormatDirectionLeadingToTrailing metrics:nil views:elementsDict]];
不包含可视格式字符串中有关两个视图中应该有多高的信息。如果它们应该相等,则使用可视格式@"V:|-[dayview1(dayview2)]-[dayview2]-|"
应该处理它。您还可以指定在末尾添加任何空格@"V:|-[dayview1]-[dayview2]-(>=20)-|"
答案 1 :(得分:0)
问题解决了: self.daysContainer = [[UIView alloc] init]; 正在创建一个新实例,因此没有在故事板中添加约束。
非常感谢和大力支持!