Reputation: 71
I am having a bit of a problem putting my image view in the middle of the cell.
I have a tableView cell with: a label which is left aligned a UIImageView (which is to be in the middle of the screen but not overlap the AccessoryView or Accessory) an optional AccessoryView or Accessory.
I then have a header cell: a label which is left aligned a UIImageView (which is to be in the middle of the screen)
The problem is that i want all the images to be aligned Horizontally (including the header) but the AccessoryView/Accessory throws off my calculation. The image view is a ScaleAspectFit. Currently i have set the width to be the full width of the contentView.
Does anyone know roughly how I would accomplish this? I have read people say to put in an empty view into the cells that won't have an AccessoryView or Accessory but this doesnt help with the Header? others have hardcoded values?
Many Thanks
Upvotes: 0
Views: 132
Reputation: 226
Change imageView frame x origin after your cell layout subviews. Just add this to your cell definition.
override func layoutSubviews() {
let screenWidth = UIScreen.main.bounds.width
let imageViewWidth = self.imageView?.frame.width
let imageViewOriginX = (screenWidth - imageViewWidth!) / 2
self.customImageView.frame.origin.x = imageViewOriginX
}
Upvotes: 0