【问题标题】:clipsToBounds and masksToBounds in grouped UITableViewCell分组 UITableViewCell 中的 clipsToBounds 和 maskToBounds
【发布时间】:2013-02-20 16:23:16
【问题描述】:

我试图让我的UITextView 的角被包含它的分组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];

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

【问题讨论】:

  • 你试过cell.clipsToBounds = YES;吗?
  • 是的,但这不是我需要的。 clipsToBounds 表示单元格将隐藏其父级之外的部分图层。我希望UITextView 将其图层的部分隐藏在其父级之外(单元格内容视图/单元格本身)
  • contentView 的边界大于单元格的边界,因此剪切 contentview 并不能解决您的问题 - 您必须剪切到单元格
  • 您在使用情节提要吗?我认为您可以在单元格上检查Clip Subviews,但我不确定如何在代码中执行此操作。
  • Pfitz,在单元格或 cell.contentView 级别进行屏蔽没有区别。也不会将 detailTextView 添加为一个或另一个的子视图。

标签: ios uikit quartz-core cglayer


【解决方案1】:

我很确定您不能以这种方式掩盖角落。单元格的backgroundView 是分组UITableView 的图像,因此没有掩蔽感。

解决问题的一个可能方法是自己解决问题。这有点棘手,因为您只想圆顶单元格的顶角和底部单元格的底角。幸运的是,@lomanf 在这里发布了一个很好的解决任意角的解决方案:Round two corners in 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;
}        

【讨论】:

    【解决方案2】:

    我也遇到了同样的问题

    不同之处在于我使用的是普通样式,并且我正在尝试使每个单元格都带有圆角。最后,我做到了,这是我的方式:

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

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

    2。 将您的自定义 TableViewCell 的 contentview 的背景颜色设置为白色,然后将您的 tableView 的背景颜色设置为清除颜色。就是这样。

    【讨论】:

      猜你喜欢
      • 2017-01-20
      • 2023-02-23
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-07-08
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多