You can change the color (and whatever you want) as the section title in the scrollViewDidScroll method. This example dims the color of the floating header when the user scrolls down and stores this white value between 0.9 and 0.6. It also displays the lower border in the title if you scroll down more than 5 points.
.M file for RDHeaderView:
- (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;
}
Relevant methods in the table view controller:
-(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) > 5)? NO: YES;
CGFloat colorOffset = fmaxf(0.6, 0.9 - (scrollView.contentOffset.y - sectionYOffset)/1000.0);
if (colorOffset > 0.9) colorOffset = 0.9;
pinnedHeader.contentView.backgroundColor = [UIColor colorWithWhite:colorOffset alpha:1];
}
-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section {
return 80;
}
source
share