0

I am trying to put an image in the center of a table view cell using constraints programmatically.

I have taken an working example from the internet and updated it to this:

contentView.addConstraints(NSLayoutConstraint.constraintsWithVisualFormat("H:|-(<=1)-[image(10)]", options: NSLayoutFormatOptions.AlignAllCenterX, metrics: nil, views: viewsDict))

What am I doing wrong? The image disappears from the table view cell when I set this.

asheyla
  • 3,175
  • 5
  • 18
  • 34

1 Answers1

0

Is it possible your viewsDict is set up incorrectly? Try this

// Center horizontally
var constraints = NSLayoutConstraint.constraintsWithVisualFormat(
  "V:[superview]-(<=1)-[image]",
  options: NSLayoutFormatOptions.AlignAllCenterX,
  metrics: nil,
  views: ["superview":contentView, "image":imageView])

contentView.addConstraints(constraints)

The other possible problem is if the contentView is not set up with constraints to make it the right size. Take a look at the contentView and see what it's size is.

wottle
  • 13,095
  • 4
  • 27
  • 68