居中容器视图

时间:2015-09-30 08:00:49

标签: ios layout autolayout ios-autolayout visual-format-language

//屏幕上有3个容器。顶部/中部/底部(我在这里只添加了中间代码)

- (void)initLayoutConstrains
{
[self initMiddleLayoutContainerConstraint];
NSMutableArray *constraints = [[NSMutableArray alloc] init];
NSDictionary *viewsDictionary =     NSDictionaryOfVariableBindings(_topContainerBar, _bottomContainerBar, _middleContainerBar);
[constraints addObjectsFromArray:[NSLayoutConstraint constraintsWithVisualFormat:@"|-(>=20)-[_middleContainerBar(==200)]-(>=20)-|"
                                                               options: NSLayoutFormatAlignAllCenterX | NSLayoutFormatAlignAllCenterY
                                                               metrics:nil
                                                                 views:viewsDictionary]];

1 个答案:

答案 0 :(得分:0)

编辑:我没有太多时间,但这是我提出的临时解决方案:

CGFloat testViewHeight = 200;
NSDictionary *metrics = @{ @"testViewY": @(self.view.frame.size.height / 2 - testViewHeight / 2), @"testViewHeight":@(testViewHeight)};

[constraints addObjectsFromArray:[NSLayoutConstraint constraintsWithVisualFormat:@"V:|-testViewY-[testView(==testViewHeight)]"
                                                                         options: kNilOptions
                                                                         metrics:metrics
                                                                           views:viewsDictionary]];

[constraints addObjectsFromArray:[NSLayoutConstraint constraintsWithVisualFormat:@"H:|[testView]|" options:kNilOptions metrics:nil views:viewsDictionary]];