我在使用UIBezierPath时,遇到了UITableViewCell中自动布局约束的问题。我的单元格无法达到全屏宽度,您可以看到图像1和图像2之间的差异。在图像2中,我没有添加圆角。
图像1
图像2
以前,在UIView中我也遇到了UIBezierPath同样的问题(您可以看到前两个带有“0”的UIView)。我使用的解决方法是像下面这样在viewDidLayoutSubviews中掩盖圆角:
- (void)viewDidLayoutSubviews {
[super viewDidLayoutSubviews];
[self.view layoutIfNeeded];
UIView *container = (UIView *)[self.view viewWithTag:100101];
[container setBackgroundColor:[UIColor colorWithHexString:@"ffffff"]];
[self setMaskTo:container byRoundingCorners:UIRectCornerAllCorners];
}
但是现在我被困在UITableViewCell中,因为我无法在viewDidLayoutSubviews中添加圆角。我的代码如下:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = @"myData";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
}
MyWClass *w = [_wData objectAtIndex:indexPath.row];
UIView *container = (UIView *) [cell viewWithTag:200001];
[container setBackgroundColor:[UIColor colorWithHexString:w.bgColor]];
UIBezierPath *maskPath = [UIBezierPath bezierPathWithRoundedRect:container.layer.bounds
byRoundingCorners:UIRectCornerTopLeft | UIRectCornerTopRight
cornerRadii:CGSizeMake(10.0, 10.0)];
CAShapeLayer *maskLayer = [CAShapeLayer layer];
maskLayer.frame = container.bounds;
maskLayer.path = maskPath.CGPath;
container.layer.mask = maskLayer;
[cell setBackgroundColor:[UIColor colorWithHexString:@"#efeff4"]];
cell.layer.masksToBounds = NO;
cell.layer.shadowOpacity = 1.0;
cell.layer.shadowOffset = CGSizeMake(0, 2);
cell.layer.shadowColor = [UIColor colorWithHexString:@"#e4e4e8"].CGColor;
cell.layer.shadowRadius = 0;
return cell;
}
我尝试添加 [cell.contentView layoutIfNeeded];
但仍然没有变化。
任何帮助都将不胜感激。
[UIBezierPath bezierPathWithRoundedRect
创建的双侧圆角代码从- (void)awakeFromNib
方法中移除到- (void)drawRect:(CGRect)rect
方法中的customTableViewCell
,现在已经可以正常工作了。 :) - Tulon