【发布时间】:2015-08-30 14:28:55
【问题描述】:
与 UIBezierPath 一起使用时,我遇到了 UITableViewCell 中的自动布局约束问题。我的细胞不会全宽。您可以看到图 1 和图 2 之间的不同。图 2 我没有添加圆角。
图片 1
图片 2
以前,我在 UiView 中遇到了与 UIBezierPath 相同的问题(您可以看到前 2 个带有“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];,但还是一样。
任何帮助将不胜感激。
【问题讨论】:
-
container.layer.masksToBounds = YES;或-clipsToBounds -
@0yeoj 试过了,还是不行
-
如果您将单元格滚动到屏幕外,当您向后滚动它们时它们会自行修复吗?
-
@stefandouganhyde no
-
你能用标签
200001发布你创建视图的部分吗?
标签: ios objective-c uitableview autolayout