0

I'm attempting to create a custom TableView cell that contains a UIImageView with margins. No matter what I try, the top and bottom margins collapse. I've read through various threads here and around the web, and I'm at a loss for what I'm doing wrong. At this point, I have to assume there is a fundamental flaw in how I'm thinking about this problem. Can anyone here help me?

Here is my current setup:

enter image description here

And here is part of the output in the simulator:

enter image description here

Eric Aya
  • 69,473
  • 35
  • 181
  • 253
cmilr
  • 379
  • 4
  • 13
  • Are there any autolayout errors in the console? I’m suspecting that’s because of this issue: https://stackoverflow.com/q/25059443/3151675. TLDR: try setting the priority of the height constraint to 999. – Tamás Sengel Oct 08 '18 at 18:17
  • Thanks for the reply. I've already got it set that way. It does fix an error that was showing up, but doesn't correct the margin problem. – cmilr Oct 08 '18 at 18:20
  • Is the height of the table view cells set to "Automatic Dimension"? – Tamás Sengel Oct 08 '18 at 18:27
  • Row Height is set to auto in the Storyboard. Is there somewhere else I have to set it? – cmilr Oct 08 '18 at 18:54
  • Screw the `UIImageView` piece - if you have a simple `UIView` in the cell, is anything different? Let's not complicate anything more than it needs to be. :-) –  Oct 08 '18 at 19:20
  • Ok yeah, if I remove the imageview and add a UIView with the same constraints it works fine, with a nice margin. – cmilr Oct 08 '18 at 19:37
  • Based on the UIView experiment, it appears that the margins are getting created correctly, but the images are displaying out over the top of them. – cmilr Oct 08 '18 at 19:43
  • 1
    cmilr :: I know this is not what you want to hear, but I strongly recommend abandoning Storyboards and the IB. Once you get to more advanced UIs you will be glad you did. Program your interface in code--don't waste your time with IB. Brian Voong has some really good tutorials on youtube showing how easy and effective it is to just work in code. It still takes time--it is verbose, but in the end you will be glad you did. Happy Coding. – Barns Oct 08 '18 at 21:41
  • Thanks @Barns, and I know where you're coming from. But I'm looking for an iOS Dev job, and need to be proficient in both forms. – cmilr Oct 08 '18 at 21:49
  • Yes, I get that! Good Luck! – Barns Oct 08 '18 at 21:53

1 Answers1

0

Thank you for your help. This turned out to be simple user error, though it would have been nice to get a warning from the compiler ;)

In my ViewController, I wasn't setting my Custom Cell's profileImage.image property—I was instead setting the image on some default imageView property, which I wasn't even aware existed.

The default imageView of course did not make use of the constraints. Now I need to do some research on that default imageView... doh!

enter image description here

cmilr
  • 379
  • 4
  • 13