如何设置自定义单元格标签的约束?

时间:2015-12-09 12:00:29

标签: ios objective-c uilabel nslayoutconstraint

  1. 我有uilabel,它是通过在tableview单元类中提到的编程宽度和高度创建的。

  2. 现在我想为那个uilabel添加约束。

  3. 3.i希望尾随空间到内容视图并将空间引导到内容视图,然后是顶部和底部空间到内容视图。

    4.我知道如何通过故事板创建约束,但结构以编程方式完成。

    这就是我的尝试。

     [cell.contentView addConstraint:[NSLayoutConstraint constraintWithItem:cell.lblDisplay attribute:NSLayoutAttributeCenterX relatedBy:NSLayoutRelationEqual toItem:self attribute:NSLayoutAttributeCenterX multiplier:1.0 constant:0.0]];
    

    请有人帮我设置上面提到的限制....

3 个答案:

答案 0 :(得分:0)

你的代码无处谈论视图的高度和宽度,而只是提到了位置。要使约束有效,您需要视图的高度,宽度,XPosition和YPosition。你可以简单地按照这个标准,你标签的标准间距就可以了。

NSDictionary *viewsDictionary = NSDictionaryOfVariableBindings(contentView);
NSArray *constraintsV = [NSLayoutConstraint constraintsWithVisualFormat:@"V:|-[contentView]-|" options:0 metrics:nil views:viewsDictionary];

[containerView addConstraints:constraintsV];

viewsDictionary = NSDictionaryOfVariableBindings(contentView);
NSArray *constraintsH = [NSLayoutConstraint constraintsWithVisualFormat:@"H:|-[contentView]-|" options:0 metrics:nil views:viewsDictionary];

[containerView addConstraints:constraintsH];

答案 1 :(得分:0)

UILabel *label = [UILabel new];
label.translatesAutoresizingMaskIntoConstraints = NO;
[self.contentView addSubview:label];
[self.contentView addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"H:|-leadingSpace-[label]-trailingSpace-|" options:0
                                                                         metrics:@{@"leadingSpace": @(10),
                                                                                   @"trailingSpace": @(10)}
                                                                           views:NSDictionaryOfVariableBindings(label)]];
[self.contentView addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"V:|-topSpace-[label]-bottomSpace-|" options:0
                                                                         metrics:@{@"topSpace": @(10),
                                                                                   @"bottomSpace": @(10)}
                                                                           views:NSDictionaryOfVariableBindings(label)]];

答案 2 :(得分:0)

Masonry提供了以编程方式添加约束的更简单方法。

LEt's说你有一个UIView并且想要为它添加约束:

   UIView *superview = self;

UIView *view1 = [[UIView alloc] init];
view1.translatesAutoresizingMaskIntoConstraints = NO;
view1.backgroundColor = [UIColor greenColor];
[superview addSubview:view1];

UIEdgeInsets padding = UIEdgeInsetsMake(10, 10, 10, 10);

[superview addConstraints:@[

    //view1 constraints
    [NSLayoutConstraint constraintWithItem:view1
                                 attribute:NSLayoutAttributeTop
                                 relatedBy:NSLayoutRelationEqual
                                    toItem:superview
                                 attribute:NSLayoutAttributeTop
                                multiplier:1.0
                                  constant:padding.top],

    [NSLayoutConstraint constraintWithItem:view1
                                 attribute:NSLayoutAttributeLeft
                                 relatedBy:NSLayoutRelationEqual
                                    toItem:superview
                                 attribute:NSLayoutAttributeLeft
                                multiplier:1.0
                                  constant:padding.left],

    [NSLayoutConstraint constraintWithItem:view1
                                 attribute:NSLayoutAttributeBottom
                                 relatedBy:NSLayoutRelationEqual
                                    toItem:superview
                                 attribute:NSLayoutAttributeBottom
                                multiplier:1.0
                                  constant:-padding.bottom],

    [NSLayoutConstraint constraintWithItem:view1
                                 attribute:NSLayoutAttributeRight
                                 relatedBy:NSLayoutRelationEqual
                                    toItem:superview
                                 attribute:NSLayoutAttributeRight
                                multiplier:1
                                  constant:-padding.right],

 ]];

使用以下代码的砌体可以完成同样的操作:

UIEdgeInsets padding = UIEdgeInsetsMake(10, 10, 10, 10);

[view1 mas_makeConstraints:^(MASConstraintMaker *make) {
    make.top.equalTo(superview.mas_top).with.offset(padding.top); //with is an optional semantic filler
    make.left.equalTo(superview.mas_left).with.offset(padding.left);
    make.bottom.equalTo(superview.mas_bottom).with.offset(-padding.bottom);
    make.right.equalTo(superview.mas_right).with.offset(-padding.right);
}];

甚至更短

[view1 mas_makeConstraints:^(MASConstraintMaker *make) {
    make.edges.equalTo(superview).with.insets(padding);
}];