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

How to set the constraints for custom cell label?

  1. 我有 uilabel ,它是通过表格视图单元格 class 中提到的编程宽度和高度创建的。

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

3.i 想要尾随 space 到内容视图并引导 space 到内容视图,然后顶部和底部 space 到内容视图。

4.i 我知道如何通过情节提要创建约束,但结构以编程方式完成。

这是我试过的。

 [cell.contentView addConstraint:[NSLayoutConstraint constraintWithItem:cell.lblDisplay attribute:NSLayoutAttributeCenterX relatedBy:NSLayoutRelationEqual toItem:self attribute:NSLayoutAttributeCenterX multiplier:1.0 constant: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];
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)]];

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

假设您有一个 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);
}];