iOS以编程方式设置UI,NSLayoutConstraint报告问题,但无法找到它

时间:2015-04-27 08:16:55

标签: ios layout autolayout nsautolayout

这个问题困扰了我很多天。我无法理解为什么会这样。

UI的所有内容都是以编程方式完成的。此视图init和layoutSubview定义UI,并添加它。

标签设计在左侧,ImageView放在右侧。所有的填充和空间都是2.

这是我的代码:

override init(style: UITableViewCellStyle, reuseIdentifier: String?) {
    super.init(style: style, reuseIdentifier: reuseIdentifier)
    println("In Cell init!")

    viewsDictionary = ["title":title,"subDescription":subDescription,"image":imageView]

    if(autolayoutSet == false) {
    self.title = UILabel(frame: CGRectZero)
    self.title.numberOfLines = 1
    self.title.textColor = UIColor.blueColor()
    self.subDescription = UILabel(frame: CGRectZero)
    self.subDescription.numberOfLines = 0
    self.subDescription.lineBreakMode = NSLineBreakMode.ByWordWrapping
    nailImage = UIImageView(frame: CGRectZero)

    self.contentView.addSubview(title)
    self.contentView.addSubview(subDescription)
    self.contentView.addSubview(nailImage)

    self.autoresizingMask = UIViewAutoresizing.FlexibleWidth | UIViewAutoresizing.FlexibleHeight
    }
}

override func layoutSubviews() {
    super.layoutSubviews()

    println("In Cell layoutSubView!")

        self.contentView.frame = self.bounds

        self.title.frame = CGRectMake(leftMargin, topMargin, self.titleWidth, self.titleHeight)
        self.subDescription.frame = CGRectMake(leftMargin, (topMargin+self.titleHeight+interMargin), self.subDescriptionWidth, self.subDescriptionHeight)
        self.nailImage.frame = CGRectMake((self.frame.width - ImageWidth - rightMargin), topMargin, ImageWidth, ImageWidth)

        println("Title \(self.title.text) cell layoutSubViewcounter \(self.layoutSubViewcounter)")
        layoutSubViewcounter++
}

func updateCellConstraints() -> CGFloat 
  {
              self.contentView.setTranslatesAutoresizingMaskIntoConstraints(false)

    title_constraint_H = NSLayoutConstraint(item: self.title, attribute: NSLayoutAttribute.Leading, relatedBy: NSLayoutRelation.Equal, toItem: self.contentView, attribute: NSLayoutAttribute.Leading, multiplier: 1.0, constant: 2)

    subDesc_constraint_H = NSLayoutConstraint(item: self.subDescription, attribute: NSLayoutAttribute.Leading, relatedBy: NSLayoutRelation.Equal, toItem: self.contentView, attribute: NSLayoutAttribute.Leading, multiplier: 1.0, constant: 2)
    image_constraint_H = NSLayoutConstraint(item: self.nailImage, attribute: NSLayoutAttribute.Trailing, relatedBy: NSLayoutRelation.Equal, toItem: self.contentView, attribute: NSLayoutAttribute.Trailing, multiplier: 1.0, constant: 2)
    image_constraint_V = NSLayoutConstraint(item: self.nailImage, attribute: NSLayoutAttribute.Top, relatedBy: NSLayoutRelation.Equal, toItem: self.contentView, attribute: NSLayoutAttribute.Top, multiplier: 1.0, constant: 2.0)

    subDesc_constraint_V = NSLayoutConstraint(item: self.subDescription, attribute: NSLayoutAttribute.Top, relatedBy: NSLayoutRelation.Equal, toItem: self.title, attribute: NSLayoutAttribute.Bottom, multiplier: 1.0, constant: 2.0)

     title_width_constraint = NSLayoutConstraint(item: self.title, attribute: NSLayoutAttribute.Width, relatedBy: NSLayoutRelation.Equal, toItem: nil, attribute: NSLayoutAttribute.NotAnAttribute, multiplier: 1.0, constant: titleWidth)
     desc_width_constraint = NSLayoutConstraint(item: self.subDescription, attribute: NSLayoutAttribute.Width, relatedBy: NSLayoutRelation.Equal, toItem: nil, attribute: NSLayoutAttribute.NotAnAttribute, multiplier: 1.0, constant: subDescriptionWidth)
     image_width_constraint = NSLayoutConstraint(item: self.nailImage, attribute: NSLayoutAttribute.Width, relatedBy: NSLayoutRelation.Equal, toItem: nil, attribute: NSLayoutAttribute.NotAnAttribute, multiplier: 1.0, constant: ImageWidth)
     image_height_constraint = NSLayoutConstraint(item: self.nailImage, attribute: NSLayoutAttribute.Height, relatedBy: NSLayoutRelation.Equal, toItem: nil, attribute: NSLayoutAttribute.NotAnAttribute, multiplier: 1.0, constant: ImageWidth)

    self.contentView.addConstraint(title_constraint_H!)
    self.contentView.addConstraint(subDesc_constraint_H!)
    self.contentView.addConstraint(subDesc_constraint_V!)
    self.contentView.addConstraint(image_constraint_H!)
    self.contentView.addConstraint(image_constraint_V!)
    self.contentView.addConstraint(title_width_constraint!)
    self.contentView.addConstraint(desc_width_constraint!)
    self.contentView.addConstraint(image_width_constraint!)
    self.contentView.addConstraint(image_height_constraint!)
}

测试结果报告问题:

2015-04-27 17:24:21.120 Swift_UI_programmatically[25239:5577543] Unable to simultaneously satisfy constraints.

NSAutoresizingMaskLayoutConstraints that you don't understand, refer to the documentation for the UIView property translatesAutoresizingMaskIntoConstraints) 
(
    "NSLayoutConstraint:0x7ff8c24b6df0 H:|-(2)-[UILabel:0x7ff8c24b9830'It is a well known fact t...']   (Names: '|':UITableViewCellContentView:0x7ff8c24ac020 )",
    "NSLayoutConstraint:0x7ff8c24b39d0 H:[UILabel:0x7ff8c24b9830'It is a well known fact t...'(220)]",
    "NSAutoresizingMaskLayoutConstraint:0x7ff8c2783d70 h=--& v=--& UILabel:0x7ff8c24b9830'It is a well known fact t...'.midX == + 107.75"
)

Will attempt to recover by breaking constraint 
NSLayoutConstraint:0x7ff8c24b6df0 H:|-(2)-[UILabel:0x7ff8c24b9830'It is a well known fact t...']   (Names: '|':UITableViewCellContentView:0x7ff8c24ac020 )

Make a symbolic breakpoint at UIViewAlertForUnsatisfiableConstraints to catch this in the debugger.
The methods in the UIConstraintBasedLayoutDebugging category on UIView listed in UIKit/UIView.h may also be helpful.
2015-04-27 17:24:21.121 Swift_UI_programmatically[25239:5577543] Unable to simultaneously satisfy constraints.
    Probably at least one of the constraints in the following list is one you don't want. Try this: (1) look at each constraint and try to figure out which you don't expect; (2) find the code that added the unwanted constraint or constraints and fix it. (Note: If you're seeing NSAutoresizingMaskLayoutConstraints that you don't understand, refer to the documentation for the UIView property translatesAutoresizingMaskIntoConstraints) 
(
    "NSLayoutConstraint:0x7ff8c24b39d0 H:[UILabel:0x7ff8c24b9830'It is a well known fact t...'(220)]",
    "NSAutoresizingMaskLayoutConstraint:0x7ff8c2783f10 h=--& v=--& H:[UILabel:0x7ff8c24b9830'It is a well known fact t...'(211.5)]"
)

1 个答案:

答案 0 :(得分:0)

我认为问题出在你的Autoresizing面具上。您已将标签与固定宽度的内容视图左侧对齐,并且还给出了NSAutoresizingMaskLayoutConstraint,它将标签的水平中心指定为107.75。

尝试为UILabel和单元格内的其他元素设置属性translatesAutoresizingMaskIntoConstraints为NO。

希望有所帮助:)