使用可视格式语言约束2个视图

时间:2015-05-20 14:26:28

标签: objective-c autolayout uikit

我有两个视图,我希望view1具有零到view2的顶部,底部,尾部和前导空格。我如何才能意识到使用可视化格式语言? 我知道我必须创建一个字典,为视图分配一个字符串,然后我能够定义,但就是这样。如果能得到如何做到这一点会很好。

// Creating my dictionary
NSDictionary * viewsDictionary = NSDictionaryOfVariableBindings(childController.view,self.containerView);

// How to define the relation i described above ?
NSArray constraints = [NSLayoutconstrain constraintWithVisualFormat:...] 

// How to add the constraints to the actual view ?

1 个答案:

答案 0 :(得分:1)

假设你有两个UIView名为view1和view2 -

[view1 addSubview:view2];

NSDictionary *views = @{@"view2" : view2};
NSArray *horzConstraints = [NSLayoutConstraint constraintsWithVisualFormat:@"H:|[view2]|"
                                                                   options:0
                                                                   metrics:nil
                                                                     views:views];

NSArray *vertConstraints = [NSLayoutConstraint constraintsWithVisualFormat:@"V:|[view2]|"
                                                                   options:0
                                                                   metrics:nil
                                                                     views:views];

[view1 addConstraints:horzConstraints];
[view1 addConstraints:vertConstraints];

view2.translatesAutoresizingMaskIntoConstraints = NO;