简体   繁体   中英

UITableviewCell Bottom Left & Right corner radius

I have applied bottom left and right corner radius to my UITableViewCell and it works fine but the width of that cell is decreased.I have constraints applied in the containerView of that cell . Whats the reason behind this ?

在此处输入图片说明

- (void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath
{
    RateChartTableViewCell *cell1 = (RateChartTableViewCell *)cell;

    if(indexPath.section == 0 && indexPath.row == 4)
    {
        [tableView setSeparatorStyle:UITableViewCellSeparatorStyleNone];
        UIBezierPath *maskPath = [UIBezierPath 
    bezierPathWithRoundedRect:cell1.containerView.bounds byRoundingCorners:( UIRectCornerBottomLeft | UIRectCornerBottomRight) cornerRadii:CGSizeMake(10.0, 10.0)];
        CAShapeLayer *maskLayer = [[CAShapeLayer alloc] init];
        maskLayer.frame = tableView.bounds;
        maskLayer.path  = maskPath.CGPath;
        cell1.containerView.layer.mask = maskLayer;
        cell1.containerView.clipsToBounds = YES;
    }
}

It's because cell's size is changed but maskLayer still keeps old size. In my opinion, to fix it, each time cell's size is changed, remove and add maskLayer again.

RateChartTableViewCell

@interface RateChartTableViewCell : UITableViewCell

@property(nonatomic, strong) CAShapeLayer* maskLayer;
... other properties

@end

@implementation RateChartTableViewCell

- (void)configureBorders {
  UIBezierPath* maskPath =
  [UIBezierPath bezierPathWithRoundedRect:self.bounds
                        byRoundingCorners:(UIRectCornerBottomLeft |
                                           UIRectCornerBottomRight)
                              cornerRadii:CGSizeMake(10.0, 10.0)];
  _maskLayer = [[CAShapeLayer alloc] init];
  _maskLayer.frame = self.bounds;
  _maskLayer.path = maskPath.CGPath;
  self.layer.mask = _maskLayer;
  self.clipsToBounds = YES;
  self.backgroundColor = UIColor.redColor;
}

- (void)layoutSubviews {
  [super layoutSubviews];

  [_maskLayer removeFromSuperlayer];
  [self configureBorders];
}

- (void)prepareForReuse {
  [super prepareForReuse];

  [_maskLayer removeFromSuperlayer];
}

@end

ViewController

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
  RateChartTableViewCell* cell = // Initialize cell

  // Do other things

  if (indexPath.section == 0 && indexPath.row == 4) {
    [tableView setSeparatorStyle:UITableViewCellSeparatorStyleNone]; // You can move this line to |viewDidLoad|
    [cell configureBorders];
  }

  return cell;
}

maskLayer.frame = tableView.bounds; maybe wrong. You can change it: maskLayer.frame = cell1.bounds; Hope it will help you.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM