AutoLayout试图获得具有相等水平空间的固定宽度和高度

时间:2014-11-08 21:20:18

标签: ios objective-c autolayout

我在我的自定义UITableViewCell中尝试在所有按钮之间获得相等的空间。 我得到的是所有按钮都有很大的宽度和高度。 我的目标是空间相等,最大尺寸为40x40(仅通过代码),但我不知道这么做。

到目前为止我的代码:

- (void)addEqualSpaceToButtons
{
    if (_didAddEqualSpace) {
        return;
    }

    _didAddEqualSpace = YES;

    // 1
    self.btnCall.translatesAutoresizingMaskIntoConstraints = NO;
    self.btnMessage.translatesAutoresizingMaskIntoConstraints = NO;
    self.btnCalendar.translatesAutoresizingMaskIntoConstraints = NO;
    self.btnMemo.translatesAutoresizingMaskIntoConstraints = NO;
    self.btnForwardMessage.translatesAutoresizingMaskIntoConstraints = NO;
    self.btnMore.translatesAutoresizingMaskIntoConstraints = NO;
    self.btnDelete.translatesAutoresizingMaskIntoConstraints = NO;

    // 2
    NSLayoutConstraint *equalSpace = [NSLayoutConstraint constraintWithItem:self.btnDelete
                                                                  attribute:NSLayoutAttributeBottom
                                                                  relatedBy:NSLayoutRelationEqual
                                                                     toItem:self
                                                                  attribute:NSLayoutAttributeBottom
                                                                 multiplier:1
                                                                   constant:0];

    [self addConstraint:equalSpace];

    // 3
    NSDictionary *views = NSDictionaryOfVariableBindings(_btnDelete, _btnMore, _btnForwardMessage, _btnMemo, _btnCalendar, _btnMessage, _btnCall);
    NSArray *fixEqualSpace = [NSLayoutConstraint constraintsWithVisualFormat:@"H:|[_btnDelete][_btnMore(==_btnDelete)][_btnForwardMessage(==_btnDelete)][_btnMemo(==_btnDelete)][_btnCalendar(==_btnDelete)][_btnMessage(==_btnDelete)][_btnCall(==_btnDelete)]|"
                                                                     options:NSLayoutFormatAlignAllBottom
                                                                     metrics:nil
                                                                       views:views];
    [self addConstraints:fixEqualSpace];
}

0 个答案:

没有答案