Vlad Vlad - 1 month ago 9
iOS Question

Getting the coordinate of a UIImage which is a subview of a UITableViewCell

I have a UITableView. And for each UITableViewCell I have subviews such as UIImage and UILabel.

Now I want to find the exact coordinate (Preferably the CGRect) of one of the UIImages when I only have the reference to it.

One way I can think of is somehow get the coordinate of the parent UITableViewCell; Get the location of the UIImage within the cell, and then add them up.

But I am not sure if this is the best way. Isn't there an easy way to just take any UIView type and get its coordinate within the entire view, even if it's a subview of a UITableViewCell?

Answer

UIView has a set of instance methods that let you convert frames and points from one coordinate system to another. Documentation here

This could work in your parent View controller:

let convertedRect = cell.convert(cell.myImageView.frame, to: self.view)