2013-05-06 80 views
0

我想将两个视图(本例中为UIButton实例)对齐。我想要第一个按钮左对齐到它的超级视图,这很容易,但我没有看到一种方法来使第二个按钮与第一个按钮对齐,而不引用第一个按钮的宽度。使用FLKAutoLayout对齐另一个视图旁边的视图

这里就是我想现在:

UIView *superView = ...; 

    UIButton *buttonOne = [UIButton buttonWithType:UIButtonTypeCustom]; 
    buttonOne.translatesAutoresizingMaskIntoConstraints = NO; 
    [superView addView:buttonOne]; 
    [buttonOne constrainWidth:@"123" height:HEADER_HEIGHT_STRING]; 
    [buttonOne alignTop:nil leading:nil superView]; 

    UIButton *buttonTwo = [UIButton buttonWithType:UIButtonTypeCustom]; 
    buttonTwo.translatesAutoresizingMaskIntoConstraints = NO; 
    [self addSubview:buttonTwo]; 
    [buttonTwo constrainWidth:@"345" height:HEADER_HEIGHT_STRING]; 
    [buttonTwo alignLeadingEdgeWithView:buttonOne predicate:@"123"] 

如何避免在代码的最后一行@"123"?我希望它只使用buttonOne的宽度。

回答

0

我个人的答案是不使用FLKAutoLayout并向我们学习。 NSLayoutConstraint直接像这样:

NSDictionary *views = NSDictionaryOfVariableBindings(buttonOne, buttonTwo); 
[superView addConstraint:[NSLayoutConstraint constraintsWithVisualFormat:@"H:|[buttonOne][buttonTwo]-|" options:0 metrics:nil views:views]];