如何使用约束添加两个视图

时间:2016-06-02 08:29:27

标签: ios objective-c cocoa-touch autolayout ios-autolayout

我正在尝试使用相同的X,Y添加2个视图。它们使用addConstraints:[NSLayoutConstraint constraintsWithVisualFormat...]

从边缘开始有空格

我尝试了字符串"H:|20-[A][B]-20-[C]-20-[D]-20|"V:|20-[A][B]-20-[C]-20-[D]-20|以及centreX == CenterXcenterY==centerY,但他们保持冲突,认为A和B应该彼此相邻。< / p>

隐藏A,点击某个按钮,B隐藏,显示A.

1 个答案:

答案 0 :(得分:0)

要添加具有相同中心X的视图,Y您需要先使用constatins

放置其中一个视图bView(底视图)
NSArray * bVerticalConstraints = [NSLayoutConstraint constraintsWithVisualFormat:@"|-20-[bView]-20-|" options:0 metrics:nil views:NSDictionaryOfVariableBindings(bView)];
    NSArray * bHorizontalConstraints = [NSLayoutConstraint constraintsWithVisualFormat:@"V:|-20-[bView]-20-|" options:0 metrics:nil views:NSDictionaryOfVariableBindings(bView)];

这将bView置于超级视图中,并将20作为边缘偏移。

现在将tView(顶视图)放在与bView相同的中心。这里self是bView和tView的超级视图

[self addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"V:[bView]-(<=1)-[tView]" options:NSLayoutFormatAlignAllCenterX metrics:nil views:NSDictionaryOfVariableBindings(bView,tView)]];
    [self addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"H:[bView]-(<=1)-[tView]" options:NSLayoutFormatAlignAllCenterY metrics:nil views:NSDictionaryOfVariableBindings(bView,tView)]];

然后将tView的边缘固定为所需的偏移量,例如40

NSArray * tVerticalConstraints = [NSLayoutConstraint constraintsWithVisualFormat:@"|-40-[tView]-40-|" options:0 metrics:nil views:NSDictionaryOfVariableBindings(tView)];
    NSArray * tHorizontalConstraints = [NSLayoutConstraint constraintsWithVisualFormat:@"V:|-40-[tView]-40-|" options:0 metrics:nil views:NSDictionaryOfVariableBindings(tView)];

编辑:

这是怎么做的。

- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    bView = [UIView new];
    bView.backgroundColor = [UIColor redColor];
    bView.translatesAutoresizingMaskIntoConstraints = NO;

    tView = [UIView new];
    tView.backgroundColor = [UIColor blackColor];
    tView.translatesAutoresizingMaskIntoConstraints = NO;

    // Firdt bView is added then tView hence tView is exaclty above the bView.
    [self.view addSubview:bView];
    [self.view addSubview:tView];


    // Edges Constrints for bView
    [self.view addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"|-20-[bView]-20-|" options:0 metrics:nil views:NSDictionaryOfVariableBindings(bView)]];
    [self.view addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"V:|-20-[bView]-20-|" options:0 metrics:nil views:NSDictionaryOfVariableBindings(bView)]];

    // Edges Constints for tView
    [self.view addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"|-20-[tView]-20-|" options:0 metrics:nil views:NSDictionaryOfVariableBindings(tView)]];
    [self.view addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"V:|-20-[tView]-20-|" options:0 metrics:nil views:NSDictionaryOfVariableBindings(tView)]];

    // Centring for bView and tView
    [self.view addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"V:[bView]-(<=1)-[tView]" options:NSLayoutFormatAlignAllCenterX metrics:nil views:NSDictionaryOfVariableBindings(bView,tView)]];
    [self.view addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"H:[bView]-(<=1)-[tView]" options:NSLayoutFormatAlignAllCenterY metrics:nil views:NSDictionaryOfVariableBindings(bView,tView)]];

}

-(IBAction)toggleViews:(id)sender {
    NSArray * subViews = self.view.subviews;
    [self.view exchangeSubviewAtIndex:[subViews indexOfObject:tView] withSubviewAtIndex:[subViews indexOfObject:bView]];
}