【发布时间】:2015-03-10 09:33:55
【问题描述】:
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 类和 func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) 中尝试过 myImage.clipsToBounds = true,但这不影响拉伸...需要帮助.. :D
【问题讨论】:
标签: ios swift layout uiimageview constraints