How to make UITable header stick to the top of the screen when using Storyboards?

后端 未结 3 1551
感情败类
感情败类 2020-12-31 20:18

I\'ve made header using this post: Table Header Views in StoryBoards

But i am unable to make header stick (fix) to the top of the screen while scrolling.

How

3条回答
  •  轻奢々
    轻奢々 (楼主)
    2020-12-31 20:55

    Here's the code which I believe makes the header view stick to the table top (that is not it's default behavior):

    -(void)scrollViewDidScroll:(UIScrollView *)scrollView
    {
        CGRect rect = self.tableHeaderView.frame;
        rect.origin.y = MIN(0, self.tableView.contentOffset.y);
        self.tableHeaderView.frame = rect;
    }
    

    An alternative is to have header view for the first section. You can't use any subview at viewForHeaderInSection, you have to return there the view which is not yet at views hierarchy. The advantage of the method is that the section header view is designed to be at the table top, the drawback is that you might want to have headers for sections in the feature, it will break the solution.

提交回复
热议问题