Round only 2 UIView angles in custom UITableViewCell - iOS

I have UIViewa custom one UITableViewCell, and I want to combine only the lower left and right corners of this view. I am doing the following, but it does not work:

- (void)awakeFromNib {
    // Initialization code

    CAShapeLayer * maskLayer = [CAShapeLayer layer];
    maskLayer.path = [UIBezierPath bezierPathWithRoundedRect: _viewForTags.bounds byRoundingCorners: UIRectCornerBottomLeft | UIRectCornerBottomRight cornerRadii: (CGSize){7.0, 7.0}].CGPath;

    _viewForTags.layer.mask = maskLayer;
}

I usually achieve this in regular view dispatchers in a method viewWillLayoutSubviews, and it works fine, but there is no such method when I subclass UITableViewCell.

Any idea how I can get around two view angles in a subclass UITableViewCell?

+4
source share
4 answers

in fact there is a method for this condition in UITableViewCell. thislayoutSubviews

-(void)layoutSubviews
{
    CAShapeLayer * maskLayer = [CAShapeLayer layer];
    maskLayer.path = [UIBezierPath bezierPathWithRoundedRect: _im.bounds byRoundingCorners: UIRectCornerBottomLeft | UIRectCornerBottomRight cornerRadii: (CGSize){7.0, 7.0}].CGPath;

    _im.layer.mask = maskLayer;
}
+1
source

UITableViewDelgate-

tableView:willDisplayCell:forRowAtIndexPath:

, . .

+1

The reason is because you are putting your code in the wrong place. A method awakeFromNibis actually the place where your views were initialized, and at this time _ viewForTags.boundsgives you CGRectZero. You need to move the code to a method setSelected:animated:or specify a specific value CGRect.

- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];

    CAShapeLayer *maskLayer = [CAShapeLayer layer];
    maskLayer.path = [UIBezierPath bezierPathWithRoundedRect:_viewForTags.bounds byRoundingCorners:UIRectCornerBottomLeft | UIRectCornerBottomRight cornerRadii:(CGSize){7.0, 7.0}].CGPath;
    _viewForTags.layer.mask = maskLayer;
}
+1
source

Apply to cellforrowatindex

CAShapeLayer * maskLayer = [CAShapeLayer layer];
maskLayer.path = [UIBezierPath bezierPathWithRoundedRect: yourCustomCell.yourViewInCustomCell.bounds byRoundingCorners: UIRectCornerBottomLeft | UIRectCornerBottomRight cornerRadii: (CGSize){7.0, 7.0}].CGPath;

yourCustomCell.yourViewInCustomCell.layer.mask = maskLayer;
0
source

Source: https://habr.com/ru/post/1626335/


All Articles