分组UITableViewCell中的clipsToBounds和masksToBounds

问题描述 投票:0回答:2

我试图让我的

UITextView
的角被包含它的分组
UITableViewCell
的圆角所掩盖。这是当前单元格的屏幕截图

UITextView inside grouped UITableViewCell

这是我用来尝试防止角与单元格边框重叠的一些代码。我都试过了

cell.contentView.layer.masksToBounds = YES;
cell.layer.masksToBounds = YES;  //tried this as a test, still doesn't work
detailTextView.clipsToBounds = YES;
[cell.contentView addSubview:detailTextView];

cell.layer.masksToBounds = YES;
cell.contentView.layer.masksToBounds = YES;
detailTextView.clipsToBounds = YES;
[cell addSubview:detailTextView];

这显然不起作用,我错过了什么?

ios uikit quartz-core cglayer
2个回答
0
投票

我很确定你不能用这种方式掩盖角落。单元格的

backgroundView
是分组
UITableView
的图像,因此没有遮蔽感。

解决这个问题的一个可能的办法是自己解决问题。这有点棘手,因为您只想圆化顶部单元格的顶角和底部单元格的底角。幸运的是,@lomanf 在这里发布了一个很好的圆角任意角解决方案:在 UIView 中圆两个角。使用他的

MTDContextCreateRoundedMask
方法,我们就可以达到我们的目标。

- (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;
}        

0
投票

我也遇到同样的问题

不同之处在于我使用的是普通样式,并且我试图使每个单元格都带有圆角。终于,我成功了,这是我的方法:

1. 将此代码放入自定义 tableViewCell 的 awakeFromNib 方法中

    [cell.layer setMasksToBounds:YES];
    [cell.layer setCornerRadius:5.0];

2. 将自定义 TableViewCell 的 contentview 的背景颜色设置为白色,然后将 tableView 的背景颜色设置为透明颜色。仅此而已。

© www.soinside.com 2019 - 2024. All rights reserved.