简体   繁体   中英

Swift - Stretched UIImageView from programmatically constraints

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))
   }
}

This is my code I want to put myImage in center - vertically, but when I add V:|-[myImage]-| it's stretchted. I've tryed with V:|-[myImage(50)]-| , but then it's not centered vertically and I receive an error:

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>"
)

I've tryed with myImage.clipsToBounds = true in Cell class and in func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) , but thats not affect the stretching... Need help.. :D

One thing you can do is pass in a metric dictionary:

 let metrics = ["imageWidth":self.imageView.frame.width, "borderSpacing": 30]

Then pass it into the constraint:

contentView.addConstraints(NSLayoutConstraint.constraintsWithVisualFormat("H:|-[myImage(imageWidth)]-|", options: NSLayoutFormatOptions(0), metrics: metrics, views: viewsDict))

Next, you want to set this priority to required (1000), and then set the other priorities to < 1000

contentView.addConstraints(NSLayoutConstraint.constraintsWithVisualFormat("H:|-(>=borderSpacing@999)-[myImage(imageWidth@1000)]-(>=borderSpacing@999)-|", options: NSLayoutFormatOptions(0), metrics: metrics, views: viewsDict))

This is all conceptual. I did not test this code. Good luck.

Try this - I had a hard time figuring out what constraints were causing this error. Here is a simpler way to do it.

I'm using Xcode 6.1.1

"Command + A" to select all the UILabels, UIImages etc. Click Editor -> Pin > (Select...) to Superview again click Editor -> Resolve Auto Layout Issues -> Add Missing Constraints or Reset to Suggested Constraints. It depends on your case.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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