在iOS6中,我正在使用自动布局。
- 我创建了两个视图v1和v2。
- v2被添加为v1的子视图。
- v1的约束已经在编程时创建(未在此处显示)。
- 我希望v1和v2的大小相同,并且我希望v2被放置在v1的正上方,水平和垂直居中匹配,使v2正好位于v1的正上方。
问题:
- 如何实现这一点(最好使用可视化格式,如果不可能,请建议其他方法)?
代码 - 下面是我的尝试,但它没有提供所需的输出。-没有错误抛出。V1被正确放置,但V2在V1的左上角。
注意 - 我在注释中解释了我要做什么。
[v1 addSubView:v2];
v1.translatesAutoresizingMaskIntoConstraints = NO;
v2.translatesAutoresizingMaskIntoConstraints = NO;
NSDictionary *viewDictionary = NSDictionaryOfVariableBindings(v1,
v2);
//I wanted v1 and v2 to be of the same height and wanted the center Y to be aligned together
NSArray *horizontalConstraints = [NSLayoutConstraint constraintsWithVisualFormat:@"H:[v2(==v1)]"
options:NSLayoutFormatAlignAllCenterY
metrics:nil
views:viewDictionary];
//I wanted v1 and v2 to be of the same height and wanted the center X to be aligned together
NSArray *verticalConstraints = [NSLayoutConstraint constraintsWithVisualFormat:@"V:[v2(==v1)]"
options:NSLayoutFormatAlignAllCenterX
metrics:nil
views:viewDictionary];
[v1 addConstraints:horizontalConstraints];
[v1 addConstraints:verticalConstraints];