class Cell:UITableViewCell {
var myImage:UIImageView = UIImageView()
override init(style: UITableViewCellStyle, reuseIdentifier: String!) {
myImage.setTranslatesAutoresizingMaskIntoConstraints(false)
contentView.addSubview(userImage)
let viewsDict = ["contentView":contentView,"myImage":myImage]
contentView.addConstraints(NSLayoutConstraint.constraintsWithVisualFormat("V:[contentView(100)]", options: NSLayoutFormatOptions(0), metrics: nil, views: viewsDict))
contentView.addConstraints(NSLayoutConstraint.constraintsWithVisualFormat("H:|-30-[myImage]", options: NSLayoutFormatOptions(0), metrics: nil, views: viewsDict))
contentView.addConstraints(NSLayoutConstraint.constraintsWithVisualFormat("V:|-[myImage]-|", options: NSLayoutFormatOptions(0), metrics: nil, views: viewsDict))
}
}
这是我的代码我想将myImage放在中心 - 垂直,但是当我添加V:|-[myImage]-|
时,它会被拉伸。我尝试使用V:|-[myImage(50)]-|
,但它没有垂直居中,我收到错误:
2015-01-12 17:02:37.988 myApp[17463:1493388] 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:0x7a197470 V:[UITableViewCellContentView:0x7a1953d0(100)]>",
"<NSLayoutConstraint:0x7a1982f0 UIImageView:0x7a194bc0.top == UITableViewCellContentView:0x7a1953d0.topMargin>",
"<NSLayoutConstraint:0x7a198360 V:[UIImageView:0x7a194bc0(50)]>",
"<NSLayoutConstraint:0x7a198320 UITableViewCellContentView:0x7a1953d0.bottomMargin == UIImageView:0x7a194bc0.bottom>"
)
我在Cell类和myImage.clipsToBounds = true
中尝试使用func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath)
,但这不会影响拉伸...需要帮助..:D
答案 0 :(得分:0)
您可以做的一件事是传递一个度量字典:
let metrics = ["imageWidth":self.imageView.frame.width, "borderSpacing": 30]
然后将其传递给约束:
contentView.addConstraints(NSLayoutConstraint.constraintsWithVisualFormat("H:|-[myImage(imageWidth)]-|", options: NSLayoutFormatOptions(0), metrics: metrics, views: viewsDict))
接下来,您要将此优先级设置为required(1000),然后将其他优先级设置为&lt; 1000
contentView.addConstraints(NSLayoutConstraint.constraintsWithVisualFormat("H:|-(>=borderSpacing@999)-[myImage(imageWidth@1000)]-(>=borderSpacing@999)-|", options: NSLayoutFormatOptions(0), metrics: metrics, views: viewsDict))
这都是概念性的。我没有测试这段代码。祝你好运。
答案 1 :(得分:0)
试试这个 - 我很难搞清楚造成这个错误的约束条件。这是一种更简单的方法。
我正在使用Xcode 6.1.1
“Command + A”选择所有UILabels,UIImages等。 点击编辑 - &gt; Pin&gt; (选择......)到Superview 再次单击编辑器 - &gt;解决自动布局问题 - &gt;添加缺失约束或重置为建议的约束。这取决于你的情况。