How to get the absolute position of the section header in a table view?

How to get the absolute position of the section header in the table view?

I already used tableView:viewForHeaderInSection: to get the title. But now I need to get the absolute position of this view on the screen. the frame of the return value is not an absolute position on the screen. I also tried calling the supervisor, but it doesn't seem to work.

+6
source share
2 answers

tableView:viewForHeaderInSection: is a UITableViewDelegate method for creating a title view for display. This means, however, that the view has not been added to the view hierarchy, which makes it useless for your purposes.

Try

 UIView *headerView = [tableView headerViewForSection:sectionIndex]; 

instead of this. From there, you can try the following methods on the returned view to get a direct view in any other coordinate system of the view:

 [headerView convertRect:[headerView bounds] toView:[headerView window]]; 
+8
source

Assuming your top-level view directly contains a table view, which in turn contains a header:

 - (CGRect) getHeaderAbsoluteFrame : (UIView *)headerview :(UIView *)tableview { CGRect frame = headerview.frame; CGRect tableFrame = tableview.frame; float x = tableview. origin.x + headerview.frame.origin.x; float y = tableview.frame.origin.y + headerview.frame.origin.y; CGRect rect = CGRectMake (x,y,headerview.frame.size.width, headerview.frame.size.height); return rect; } 
0
source

Source: https://habr.com/ru/post/950449/


All Articles