以编程方式设置UITableViewCell的约束

时间:2017-07-26 11:06:39

标签: ios uitableview autolayout constraints

到目前为止,我一直在使用故事板来设置我的约束,但我正在尝试学习如何以编程方式执行操作。手头的问题:

表格需要返回cellForRowAt中的单元格。在那里,我只需要添加一个UILabel元素,它将约束左上角和右上角。表格单元格高度设置为自动,因为我不知道标签的大小。我的代码如下:

var uil = UILabel()

cell.addSubview(uil)
uil.leftAnchor.constraint(equalTo: cell.leftAnchor).isActive = true
uil.rightAnchor.constraint(equalTo: cell.rightAnchor).isActive = true
uil.topAnchor.constraint(equalTo: cell.topAnchor).isActive = true
uil.bottomAnchor.constraint(equalTo: cell.bottomAnchor).isActive = true
uil.numberOfLines = 0;
uil.text = "Some variable sized text that could be anything really";

如果我不向UILabel提供框架,我什么也看不见。但是,如果我使用类似的东西:

UILabel(frame: CGRect(x: 0 , y: 0 , width: 100, height: 100)

然后正如预期的那样,我看到了标签,但就像约束不适用一样。

我错过了什么?这些约束不应该是完全描述性的吗?

4 个答案:

答案 0 :(得分:1)

我认为你应该在应用约束之前添加这一行

uil.translatesAutoresizingMaskIntoConstraints = false

答案 1 :(得分:1)

如果您要动态添加视图,则需要设置view.translatesAutoresizingMaskIntoConstraints = false

有关详细信息click here

然后应用以下约束,因为它会起作用。

// align lbl from the left and right
cell.addConstraints(NSLayoutConstraint.constraintsWithVisualFormat("H:|-8-[view]-8-|", options: NSLayoutFormatOptions(rawValue: 0), metrics: nil, views: ["view": lbl]));

// align lbl from the top and bottom
cell.addConstraints(NSLayoutConstraint.constraintsWithVisualFormat("V:|-8-[view]-8-|", options: NSLayoutFormatOptions(rawValue: 0), metrics: nil, views: ["view": lbl]));

答案 2 :(得分:0)

下面是一个工作代码片段我已经在视图控制器中完成了它你可以为一个单元格做同样的事情

 var uil:UILabel!

    override func viewDidLoad() {
        super.viewDidLoad()
        self.uil = UILabel()
        self.uil?.translatesAutoresizingMaskIntoConstraints = false
        self.uil?.backgroundColor = UIColor.red
        self.uil?.text = "label text"
        self.uil.addSubview(self.uil!)
        self.uil?.leadingAnchor.constraint(equalTo: view.leadingAnchor, constant:10).isActive = true
        self.uil?.topAnchor.constraint(equalTo: view.topAnchor, constant:50).isActive = true
        self.uil?.trailingAnchor.constraint(equalTo: view.trailingAnchor, constant:-10).isActive = true
        self.uil?.heightAnchor.constraint(equalToConstant: 20.0).isActive = true


    }

答案 3 :(得分:0)

以下是我们如何应用约束的示例。我将以标签为例。

UILabel *lb1;
lb1 =[[UILabel alloc]init];
self.view addSubview:lb1];

lb1.translatesAutoresizingMaskIntoConstraints = NO;

//水平约束

NSLayoutConstraint *centreHorizontallyConstraint = [NSLayoutConstraint constraintWithItem:lb1 attribute:NSLayoutAttributeCenterX relatedBy:NSLayoutRelationEqual toItem:self.view attribute:NSLayoutAttributeCenterX multiplier:1 constant:0];

//垂直约束

NSLayoutConstraint *centreVeryConstraint = [NSLayoutConstraint constraintWithItem:lb1 attribute:NSLayoutAttributeCenterY relatedBy:NSLayoutRelationEqual toItem:self.view attribute:NSLayoutAttributeCenterY multiplier:1 constant:0];

//身高约束

NSLayoutConstraint * lblheight = [NSLayoutConstraint constraintWithItem:lb1 attribute:NSLayoutAttributeHeight relatedBy:NSLayoutRelationEqual toItem:nil attribute:NSLayoutAttributeNotAnAttribute multiplier:1.0 constant:30];

//宽度约束

NSLayoutConstraint * lblwidth = [NSLayoutConstraint constraintWithItem:lb1 attribute:NSLayoutAttributeWidth relatedBy:NSLayoutRelationEqual toItem:nil attribute:NSLayoutAttributeNotAnAttribute multiplier:1.0 constant:80];

//添加约束

[self.view addConstraints:@[centreHorizontallyConstraint,centreVeryConstraint,lblheight,lblwidth]];