简体   繁体   English

在表格视图单元格中添加自动布局约束。

[英]Adding auto layout constraints in a tableview cell.

I am trying to add a UIImageView to a cell and programmatically add auto layout constraints. 我试图将UIImageView添加到单元格并以编程方式添加自动布局约束。 However, it is giving me this following error: The view hierarchy is not prepared for the constraint... When added to a view, the constraint's items must be descendants of that view (or the view itself). This will crash if the constraint needs to be resolved before the view hierarchy is assembled. 但是,这给了我以下错误: The view hierarchy is not prepared for the constraint... When added to a view, the constraint's items must be descendants of that view (or the view itself). This will crash if the constraint needs to be resolved before the view hierarchy is assembled. The view hierarchy is not prepared for the constraint... When added to a view, the constraint's items must be descendants of that view (or the view itself). This will crash if the constraint needs to be resolved before the view hierarchy is assembled. I looked at these following posts: Swift, Constraint, The view hierarchy is not prepared for the constraint , Why is my layout constraint returning an error in this case? 我查看了以下几篇文章: Swift,Constraint,未为该约束准备视图层次结构在这种情况下为什么我的布局约束返回错误? (Swift) , and Swift, Constraint, The view hierarchy is not prepared for the constraint . (Swift)Swift,Constraint,视图层次结构没有为约束准备 One thing that I could not add to my code that these posts were suggesting me to add is setTranslatesAutoresizingMaskIntoConstraints . 我不能添加到这些帖子建议我添加的代码中的一件事是setTranslatesAutoresizingMaskIntoConstraints When I try to add this function to my imageView , I get an error. 当我尝试将此功能添加到imageView ,出现错误。

Here is my code: 这是我的代码:

    func cellTwo(indexPath: NSIndexPath) -> UITableViewCell {

        let cell = tableView.dequeueReusableCellWithIdentifier("cellTwo", forIndexPath: indexPath) as! CathyTaskLogTwoTableViewCell

        let imageView:UIImageView = UIImageView(image: UIImage(named: "defaultPicture"))
        imageView.frame.size.width = 100
        imageView.frame.size.height = 31
                let horizonalContraints = NSLayoutConstraint(item: imageView, attribute:
            .LeadingMargin, relatedBy: .Equal, toItem: cell,
            attribute: .LeadingMargin, multiplier: 1.0,
            constant: 20)
        imageView.addConstraint(horizonalContraints)

        cell.addSubview(imageView)

        return cell

    }

Thank you so much in advance for your help :) 提前非常感谢您的帮助:)

  1. The sequence is: 顺序为:

    • instantiate the subview (eg the image view); 实例化子视图(例如图像视图);
    • set translatesAutoresizingMaskIntoConstraints to false ; 设置translatesAutoresizingMaskIntoConstraintsfalse ;
    • call addSubview to add it to the view hierarchy; 调用addSubview将其添加到视图层次结构中; and
    • add the constraints. 添加约束。
  2. Do not set the frame at all if you're using constraints. 如果使用约束,则根本不要设置frame Everything should be defined by constraints. 一切都应由约束条件来定义。

  3. It is probably not prudent to call dequeueReusableCellWithIdentifier and then add a subview. 调用dequeueReusableCellWithIdentifier然后添加一个子视图可能并不明智。 What if the cell is being reused? 如果该单元被重用怎么办? You'll be adding the same subview multiple times. 您将多次添加相同的子视图。 The programmatic adding of subview is probably best put in the cell subclass implementation of awakeFromNib , if from storyboard or NIB, or init(style;, reuseIdentifier) if building it programmatically. 子视图的编程添加可能最好放在awakeFromNib的单元子类实现中(如果从情节提要或NIB中实现),或者如果以编程方式构建,则将其放入init(style;, reuseIdentifier) Or, easiest, don't programmatically create cells at all and use storyboard or NIB. 或者,最简单的是,根本不以编程方式创建单元并使用情节提要或NIB。

Before adding constraints to any view programmatically you should add it as subview and turn translatesAutoresizingMaskIntoConstraints to false and then add your required constraints. 在以编程方式将约束添加到任何视图之前,应将其添加为子视图,并将translatesAutoresizingMaskIntoConstraints设置为false,然后添加所需的约束。 Like 喜欢

 cell.addSubview(imageView)
 imageView.translatesAutoresizingMaskIntoConstraints = false

 //now add your required constraints

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM