LoginSignup
27
31

More than 5 years have passed since last update.

UITableViewでSectionHeader/SectionFooterを残さない方法

Posted at

UITableVieで、デフォルトではSectionHeader/SectionFooterが
常に見えるようにスクロールしますよね?
それを残さずに一緒にスクロールさせる方法です。

SectionHeader

SectionHeaderHeightについては、調整してください。

MyTableViewController.m
- (void)scrollViewDidScroll:(UIScrollView *)scrollView {
    // SectionHeaderの高さ
    CGFloat sectionHeaderHeight = [[self tableView] sectionHeaderHeight];
    CGFloat offsetY = [scrollView contentOffset].y;

    if ( offsetY <= sectionHeaderHeight && offsetY >= 0.f ) {
        [scrollView setContentInset:UIEdgeInsetsMake( -offsetY, 0.f, 0.f, 0.f )];
    }
    else if ( offsetY >= sectionHeaderHeight ) {
        [scrollView setContentInset:UIEdgeInsetsMake( -sectionHeaderHeight, 0.f, 0.f, 0.f )];
    }
}

SectionFooter

SectionFooterHeightについては、調整してください。

MyTableViewController.m
- (void)scrollViewDidScroll:(UIScrollView *)scrollView {
    // SectionFooterの高さ
    CGFloat sectionFooterHeight = [[self tableView] sectionFooterHeight];
    CGFloat offsetY = [scrollView contentOffset].y;
    CGFloat contentHeight = [scrollView contentSize].height;
    CGFloat bottomHeight = contentHeight - CGRectGetHeight( [scrollView bounds] );

    if ( bottomHeight - sectionFooterHeight <= offsetY && contentHeight > 0.f ) {
        [scrollView setContentInset:UIEdgeInsetsZero];
    }
    else {
        [scrollView setContentInset:UIEdgeInsetsMake( 0.f, 0.f, -sectionFooterHeight, 0.f )];
    }
}

初期表示時にズレが生じる場合

MyTableViewController.m
- (void)viewDidLoad {
    [super viewDidLoad];

    [self scrollViewDidScroll:[self tableView]];
}
27
31
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
27
31