I'm pretty sure that you can't mask the corners this way. The cell's backgroundView
is an image for a grouped UITableView
so there's no sense of masking.
A possible solution to the problem would be to round the corners yourself. This is a little tricky since you only want to round the top corners of the top cell and the bottom corners of the bottom cell. Fortunately, @lomanf posted a great solution to rounding arbitrary corners here: Round two corners in UIView. Using his MTDContextCreateRoundedMask
method, we can achieve our goal.
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
// Other cell instantiation
// First cell in section
if (indexPath.row == 0) {
[self roundTopOrBottomCornersOfCell:cell top:YES];
}
// Last cell in section
else if (indexPath.row == tableView.numberOfRowsInSection:indexPath.section-1) {
[self roundTopOrBottomCornersOfCell:cell top:NO];
}
}
// Modified from the second part of @lomanf's Solution 1
- (void)roundTopOrBottomCornersOfCell:(UITableViewCell*)cell top:(BOOL)top {
// Set constant radius
CGFloat radius = 5.0;
// Create the mask image you need calling @lomanf's function
UIImage* mask;
if (top) {
mask = MTDContextCreateRoundedMask(self.view.bounds, radius, radius, 0.0, 0.0);
}
else {
mask = MTDContextCreateRoundedMask(self.view.bounds, 0.0, 0.0, radius, radius);
}
// Create a new layer that will work as a mask
CALayer* layerMask = [CALayer layer];
layerMask.frame = cell.bounds;
// Put the mask image as content of the layer
layerMask.contents = (id)mask.CGImage;
// Set the mask layer as mask of the view layer
cell.layer.mask = layerMask;
}
cell.clipsToBounds = YES;
? – EngraftclipsToBounds
means that the cell would hide parts of it's layer that are outside it's parents. I want theUITextView
to hide the parts of its layer outside its parent (the cells contentView/the cell itself) – PeritoneumClip Subviews
on the cell but I am not sure how to do this in code. – Engraftcell.contentView.clipsToBounds = YES;
? I'm guessing you have, but I don't see it in the code anywhere. You might have to create a mask layer based oncell.backgroundView
. Be careful with that approach though, as it will probably reduce your frame rate, particularly while scrolling. – Telex