【问题标题】:iOS programmatically UI setting, NSLayoutConstraint report issue, but cannot find itiOS 以编程方式设置 UI,NSLayoutConstraint 报告问题,但找不到
【发布时间】:2015-07-05 14:23:28
【问题描述】:

这个问题困扰了我很多天。我不明白为什么会这样。

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

Label 被设计为放在左侧,ImageView 放在右侧。所有的 padding 和 inter-space 都是 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)]"
)

【问题讨论】:

    标签: ios layout autolayout


    【解决方案1】:

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

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

    希望对你有帮助:)

    【讨论】:

    • 非常感谢,将所有 UILable MaskIntoConstraints 设置为否后,它可以正常工作。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-05-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-03-15
    相关资源
    最近更新 更多