UITableView: change the height of the section title depending on the scroll offset

I have a simple UITableView and you want to have a β€œsticky” title (which remains where it is when the table scrolls). However, I want to change the height of the title depending on the scroll position. This means that, for example, the title is initially 100 pixels high. When you scroll down the table, the height of the header should decrease depending on the offset of the table view, until it remains at the minimum height, for example. 50px.

Here's how it should look:

enter image description hereenter image description hereenter image description hereenter image description hereenter image description here

I am already creating a quick prototype, but it does not work well. Do you have any ideas on how to make this work work and have a smooth look at the table at the same time?

That's what I'm doing:

Returns the height of the section header depending on the offset of the table view:

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section { CGFloat percentage = max(0, (SCROLL_OFFSET_HEADER_SHRINK - tableView.contentOffset.y) / SCROLL_OFFSET_HEADER_SHRINK); int newHeight = max(MIN_HEADER_HEIGHT, min(MAX_HEADER_HEIGHT, MIN_HEADER_HEIGHT + percentage * (MAX_HEADER_HEIGHT - MIN_HEADER_HEIGHT))); return newHeight; } 

Refresh the table view in each scroll event to make sure the header height changes: (which is bad for doing and leads to poor performance)

 - (void)scrollViewDidScroll:(UIScrollView *)scrollView { if ((self.tableView.contentOffset.y > SCROLL_OFFSET_HEADER_SHRINK && self.tableView.tableHeaderView.frame.size.height > MIN_HEADER_HEIGHT) || self.tableView.contentOffset.y < SCROLL_OFFSET_HEADER_SHRINK) { [self.tableView reloadData]; } } 

Any other way to set the height of the section header without having to reload the entire table helped me a lot! I would like this to work on iOS 5+.

+6
source share

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


All Articles