5

I'm having a problem with a UIImageView inside of a UITableViewCell when using autolayout. Specifically I'd like to have the image separated from the edges (20pt) , with a strict size(50pt) and so I have defined this: imageview to top = leading to edgeview = 20 height = width = 50

enter image description here

What i find is that the imageview completely ignores its constraints(sorry about the scary smiley, it's what google images gave me). Naturally my desired layout would have more content, but regardless of whether that content is present the effect is the same.

enter image description here

The setup for the project is as follows: Make a single view app Add a table view, connect the data source and write boilerplate to make at least one cell create a tableviewcell with nib, add an imageview to the tableviewcell constraints to top/leading (10 works, with a height of 20) set an image in the imageview so you can see it at runtime

I feel like that should be sufficient, but it seems not to be working, am I missing something silly? For comparison this same setup causes no problem without a tableview, the imageview behaves just the way expected

Christophe
  • 620
  • 1
  • 5
  • 13

3 Answers3

14

It turns out I had missed one step that kept Anna's version working, I had connected the UIImageView with a property named imageView (overwriting the UITableViewCell default property), apparently the constraints in the superclass were stronger than whatever I was adding.

Christophe
  • 620
  • 1
  • 5
  • 13
1

I couldn't quite get this to work myself, but I think you'll get it if you try some of the things in here:

Using Auto Layout in UITableView for dynamic cell layouts & variable row heights

Community
  • 1
  • 1
Anna Dickinson
  • 3,307
  • 24
  • 43
0

I think that it doesn't ignore it.Your setup is correct. You just have to stop drawing outside image view. Check this on ImageView: Clip Subviews ->enabled. Choose also good View fill mode: Scale To Fill or Aspect Fit.

Juraj Antas
  • 3,059
  • 27
  • 37
  • Thanks for the suggestion, I had seen the clip subviews in another post but it doesn't fix it. 'Scale to fit' is also selected by default, I tried 'Aspect fit/fill' but they don't seem to help. – Christophe Oct 31 '14 at 14:21
  • This solved my problem. I initialised the imageView with some template image with fixed size (128x128). Then, after updating the image with a camera picture, the imageView was displaying the whole picture, ignoring the restrictions 128x128. I checked the frames and nothing changed. Basically I was missing this clip stuff. Thanks! – Danilo Gomes Apr 19 '16 at 14:38