Search code examples
alignmentconstraintsxcode10

How can I add space above and below an ImageView in a row in a TableView?


The row height in this table view is 54. The ImageView Height is 44. I'd like 5 above and below the ImageView (some spacing the image so that they aren't touching each other). Basically, centering the ImageView, vertically, in the row (or ContentView). No matter what I've tried, the image is always anchored in the top left corner of the row (since its a music app, it has to run on a phone, so I don't have an image for the result). I'm embarrassed to say that I've spent hours trying to accomplish this. I'd appreciate any help. Thanks.

enter image description here


Solution

  • I was right, there was another place that was forcing the image into the top left corner. It was in the code when setting the contents of a row as shown below. I wrote that part a long time ago when I knew even less about Swift (the goal was to make sure the image gets scaled. It may not be necessary though. I'll test the code without it. Still, thank you Zaya for taking the time to help!

            let albumArtwork = rowItem.representativeItem?.artwork
            let tableImageSize = CGSize(width: 10, height: 10) // This doesn't matter, it gets resized below
            let cellImage: UIImageView = UIImageView(frame: CGRect(x: 0, y: 0, width: 44, height: 44)) // The albumImageView is 44 by 44