使用UIBezierPath时,UITableViewCell中的自动布局约束存在问题。我的细胞不会全宽。您可以看到图像1和图像2之间的差异。图像2我没有添加圆角。
图片1
图片2
以前,我在UiView中遇到了与UIBezierPath相同的问题(你可以看到前两个UIView用" 0")。我使用的解决方法是屏蔽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];
但仍然相同。
任何帮助都将不胜感激。
答案 0 :(得分:0)
以下为我工作的逻辑:
将UIview宽度设置为单元格宽度。
例如:public class Holder{
private Car car;
private Bike bike;
private List<Truck> trucks;
//getter-setter
}
答案 1 :(得分:0)
在主线程内尝试UIBezierPath
(请参见以下 Swift 5 解决方案)
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell
{
let Cell: MyTableViewCell = tableView.dequeueReusableCell(withIdentifier: "MyTableViewCell") as! MyTableViewCell
Cell.selectionStyle = .none
DispatchQueue.main.async {
let maskPath = UIBezierPath(roundedRect: Cell.contentView.bounds,
byRoundingCorners: [.topLeft, .topRight],
cornerRadii: CGSize(width: 10, height: 10))
let shape = CAShapeLayer()
shape.path = maskPath.cgPath
Cell.contentView.layer.mask = shape
}
return Cell
}
答案 2 :(得分:0)
伙计,在您的 ViewDidLoad 或 ViewDidAppear 内初始化您的 Bezier 路径,并在 cellForRow atIndexPath 上进行归因
像这样
let maskLayer = CAShapeLayer.init()
override func viewDidLoad() {
super.viewDidLoad()
self.maskLayer.path = UIBezierPath(roundedRect: self.tableView.layer.bounds, byRoundingCorners: [UIRectCorner.topLeft, UIRectCorner.topRight], cornerRadii: CGSize.init(width: 26.0, height: 26.0)).cgPath
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView .dequeueReusableCell(withIdentifier: "cellButtons", for: indexPath)
cell.layer.mask = self.maskLayer
return cell
}