【问题标题】:UITableViewCell Auto Layout Width Not Working With UIBezierPathUITableViewCell 自动布局宽度不适用于 UIBezierPath
【发布时间】: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


【解决方案1】:

以下逻辑对我有用:

将您的 UIview 宽度设置为单元格宽度。

例如:container.frame.size.width = cell.frame.size.width

【讨论】:

    【解决方案2】:

    在主线程内尝试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
                 }
    

    【讨论】:

      【解决方案3】:

      伙计,在 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
      }
      

      【讨论】:

        猜你喜欢
        • 2015-11-18
        • 1970-01-01
        • 2014-11-10
        • 1970-01-01
        • 2013-10-21
        • 2014-01-03
        • 1970-01-01
        • 2015-04-22
        • 1970-01-01
        相关资源
        最近更新 更多