Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>You can modify the color (and whatever else you want) of the section header view in the scrollViewDidScroll method. This example darkens the color of the floating header view as the user scrolls down, and keeps that color's white value between 0.9 and 0.6. It also unhides a bottom border line in the header view if you scroll down by more than 5 points.</p> <p>The .m file for RDHeaderView:</p> <pre><code>- (id)init{ self = [super init]; if (self) { UIView *line = [[UIView alloc] init]; [line setTranslatesAutoresizingMaskIntoConstraints:NO]; line.backgroundColor = [UIColor darkGrayColor]; [self addSubview:line]; [self addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"|[line]|" options:0 metrics:nil views:@{@"line":line}]]; [self addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"V:[line]|" options:0 metrics:nil views:@{@"line":line}]]; [line addConstraint:[NSLayoutConstraint constraintWithItem:line attribute:NSLayoutAttributeHeight relatedBy:0 toItem:nil attribute:NSLayoutAttributeNotAnAttribute multiplier:1 constant:1]]; self.bottomLine = line; self.bottomLine.hidden = YES; } return self; } </code></pre> <p>The relevant methods in the table view controller:</p> <pre><code>-(UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section { RDHeaderView *header = [[RDHeaderView alloc] init]; header.contentView.backgroundColor = [UIColor colorWithWhite:0.9 alpha:1]; return header; } -(void)scrollViewDidScroll:(UIScrollView *)scrollView { NSInteger topSection = [[self.tableView indexPathsForVisibleRows].firstObject section]; NSInteger sectionYOffset = [self.tableView rectForHeaderInSection:topSection].origin.y; RDHeaderView *pinnedHeader = (RDHeaderView *)[self.tableView headerViewForSection:topSection]; pinnedHeader.bottomLine.hidden = ((scrollView.contentOffset.y - sectionYOffset) &gt; 5)? NO: YES; CGFloat colorOffset = fmaxf(0.6, 0.9 - (scrollView.contentOffset.y - sectionYOffset)/1000.0); if (colorOffset &gt; 0.9) colorOffset = 0.9; pinnedHeader.contentView.backgroundColor = [UIColor colorWithWhite:colorOffset alpha:1]; } -(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section { return 80; } </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload