【问题标题】:How to set the constraints for custom cell label?如何设置自定义单元格标签的约束?
【发布时间】:2015-12-09 22:30:31
【问题描述】:
  1. 我有 uilabel,它是通过 tableview 单元类中提到的编程宽度和高度创建的。

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

3.我想要内容视图的尾随空间和内容视图的前导空间,然后内容视图的顶部和底部空间。

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

这是我尝试过的。

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

请任何人帮助我设置我上面提到的约束......

【问题讨论】:

  • 您使用过自定义单元格吗?您在哪里定制整个细胞?
  • 我正在使用第三方 tableview(即)TQMultistagetebleview,为此我创建了一个自定义 tableview 单元格,在该单元格中,一个 uilabel 可用我在 tableview 数据源方法中使用该 uilabel...跨度>
  • 使用砌体添加约束。

标签: ios objective-c uilabel nslayoutconstraint


【解决方案1】:

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],

 ]];

同样可以使用 masonry 来完成,代码如下:

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);
}];

【讨论】:

    【解决方案2】:

    您的代码没有提及视图的高度、宽度,而是仅提及位置。要使约束有效,您需要视图的高度、宽度、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];
    

    【讨论】:

    • 要使用容器视图我们需要分配容器位置我是正确的?
    • 是的,你也需要它。
    【解决方案3】:
    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)]];
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2013-06-07
      • 1970-01-01
      • 2020-11-26
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多