Last active
August 29, 2015 14:19
-
-
Save rsattar/253a01dd1984a68db673 to your computer and use it in GitHub Desktop.
How to center the cells in a table view controller on iOS 7 (like Settings.app on iPad)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
- (void) tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath | |
{ | |
// See: http://stackoverflow.com/a/18833311/9849 | |
if (self.displayAsCenteredTableView) { | |
if (tableView == self.tableView) { | |
CGFloat cornerRadius = 7.f; | |
cell.backgroundColor = UIColor.clearColor; | |
CAShapeLayer *fillLayer = [[CAShapeLayer alloc] init]; | |
CAShapeLayer *maskLayer = [[CAShapeLayer alloc] init]; | |
CGMutablePathRef pathRef = CGPathCreateMutable(); | |
CGRect bounds = CGRectInset(cell.bounds, 0, 0); | |
BOOL addLine = NO; | |
if (indexPath.row == 0 && indexPath.row == [tableView numberOfRowsInSection:indexPath.section]-1) { | |
CGPathAddRoundedRect(pathRef, nil, bounds, cornerRadius, cornerRadius); | |
} else if (indexPath.row == 0) { | |
CGPathMoveToPoint(pathRef, nil, CGRectGetMinX(bounds), CGRectGetMaxY(bounds)); | |
CGPathAddArcToPoint(pathRef, nil, CGRectGetMinX(bounds), CGRectGetMinY(bounds), CGRectGetMidX(bounds), CGRectGetMinY(bounds), cornerRadius); | |
CGPathAddArcToPoint(pathRef, nil, CGRectGetMaxX(bounds), CGRectGetMinY(bounds), CGRectGetMaxX(bounds), CGRectGetMidY(bounds), cornerRadius); | |
CGPathAddLineToPoint(pathRef, nil, CGRectGetMaxX(bounds), CGRectGetMaxY(bounds)); | |
addLine = YES; | |
} else if (indexPath.row == [tableView numberOfRowsInSection:indexPath.section]-1) { | |
CGPathMoveToPoint(pathRef, nil, CGRectGetMinX(bounds), CGRectGetMinY(bounds)); | |
CGPathAddArcToPoint(pathRef, nil, CGRectGetMinX(bounds), CGRectGetMaxY(bounds), CGRectGetMidX(bounds), CGRectGetMaxY(bounds), cornerRadius); | |
CGPathAddArcToPoint(pathRef, nil, CGRectGetMaxX(bounds), CGRectGetMaxY(bounds), CGRectGetMaxX(bounds), CGRectGetMidY(bounds), cornerRadius); | |
CGPathAddLineToPoint(pathRef, nil, CGRectGetMaxX(bounds), CGRectGetMinY(bounds)); | |
} else { | |
CGPathAddRect(pathRef, nil, bounds); | |
addLine = YES; | |
} | |
fillLayer.path = pathRef; | |
CGPathRef pathCopyRef = CGPathCreateCopy(pathRef); | |
maskLayer.path = pathCopyRef; | |
CFRelease(pathRef); | |
CFRelease(pathCopyRef); | |
fillLayer.fillColor = [UIColor colorWithWhite:1.f alpha:0.8f].CGColor; | |
if (addLine == YES) { | |
CALayer *lineLayer = [[CALayer alloc] init]; | |
CGFloat lineHeight = (1.f / [UIScreen mainScreen].scale); | |
lineLayer.frame = CGRectMake(CGRectGetMinX(bounds)+10, bounds.size.height-lineHeight, bounds.size.width-10, lineHeight); | |
lineLayer.backgroundColor = tableView.separatorColor.CGColor; | |
[fillLayer addSublayer:lineLayer]; | |
} | |
UIView *testView = [[UIView alloc] initWithFrame:bounds]; | |
[testView.layer insertSublayer:fillLayer atIndex:0]; | |
testView.backgroundColor = UIColor.clearColor; | |
cell.backgroundView = testView; | |
cell.layer.mask = maskLayer; | |
} | |
} else { | |
cell.backgroundColor = [UIColor whiteColor]; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment