Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to mask UITableViewCells underneath a UITableView Transparent Header

I want the header to mask the cells, but not the background.

I have a UITableView with transparent headers and cells similar to Apple's Notification Center (when you swipe down on the status bar on your iPhone). I can't figure out how to mask the cells so they don't show up underneath the header when it scrolls.

I've tried changing the contentInsets of the tableview, and I've tried changing the frame of the header View to a negative origin.

like image 327
Jesse Gumpo Avatar asked Aug 26 '12 02:08

Jesse Gumpo


2 Answers

Try to make a subclass of UITableviewCell and add these methods

- (void)maskCellFromTop:(CGFloat)margin {     self.layer.mask = [self visibilityMaskWithLocation:margin/self.frame.size.height];     self.layer.masksToBounds = YES; }  - (CAGradientLayer *)visibilityMaskWithLocation:(CGFloat)location {     CAGradientLayer *mask = [CAGradientLayer layer];     mask.frame = self.bounds;     mask.colors = [NSArray arrayWithObjects:(id)[[UIColor colorWithWhite:1 alpha:0] CGColor], (id)[[UIColor colorWithWhite:1 alpha:1] CGColor], nil];     mask.locations = [NSArray arrayWithObjects:[NSNumber numberWithFloat:location], [NSNumber numberWithFloat:location], nil];     return mask; } 

and add this delegate method in UITableView

#pragma mark - UIScrollViewDelegate  - (void)scrollViewDidScroll:(UIScrollView *)scrollView {     for (iNotifyTableViewCell *cell in self.visibleCells) {         CGFloat hiddenFrameHeight = scrollView.contentOffset.y + [iNotifyHeaderView height] - cell.frame.origin.y;         if (hiddenFrameHeight >= 0 || hiddenFrameHeight <= cell.frame.size.height) {             [cell maskCellFromTop:hiddenFrameHeight];         }     } } 

*Note that [iNotifyHeaderView height] is the height of the HeaderView. and use #import <QuartzCore/QuartzCore.h> for the custom cell.

like image 59
Alex Markman Avatar answered Sep 19 '22 09:09

Alex Markman


A little edit on Alex Markman's answer, where you could skip creating a subclass for an UITableViewCell. Benefit of this approach is that you can use it for multiple different UITableViewCells.

- (void)scrollViewDidScroll:(UIScrollView *)scrollView {     for (UITableViewCell *cell in self.tableView.visibleCells) {         CGFloat hiddenFrameHeight = scrollView.contentOffset.y + self.navigationController.navigationBar.frame.size.height - cell.frame.origin.y;         if (hiddenFrameHeight >= 0 || hiddenFrameHeight <= cell.frame.size.height) {             [self maskCell:cell fromTopWithMargin:hiddenFrameHeight];         }     } }  - (void)maskCell:(UITableViewCell *)cell fromTopWithMargin:(CGFloat)margin {     cell.layer.mask = [self visibilityMaskForCell:cell withLocation:margin/cell.frame.size.height];     cell.layer.masksToBounds = YES; }  - (CAGradientLayer *)visibilityMaskForCell:(UITableViewCell *)cell withLocation:(CGFloat)location {     CAGradientLayer *mask = [CAGradientLayer layer];     mask.frame = cell.bounds;     mask.colors = [NSArray arrayWithObjects:(id)[[UIColor colorWithWhite:1 alpha:0] CGColor], (id)[[UIColor colorWithWhite:1 alpha:1] CGColor], nil];     mask.locations = [NSArray arrayWithObjects:[NSNumber numberWithFloat:location], [NSNumber numberWithFloat:location], nil];     return mask; } 
like image 24
Antoine Avatar answered Sep 22 '22 09:09

Antoine