Round only 2 corners of a UIView in custom UITableViewCell - iOS
Asked Answered
R

5

6

I have a UIView in a custom UITableViewCell and I want to round just bottom Left and Right corners of that view. I'm doing the following, but it's not working:

- (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 usual View Controllers in the viewWillLayoutSubviews method and it works perfectly, but there's no such method when I subclass UITableViewCell.

Any idea how can I round 2 corners of a view in a subclassed UITableViewCell?

Ruprecht answered 28/1, 2016 at 11:26 Comment(4)
once try in cellrowatindexpath and tryHedve
Tried. Doesn't work :(Ruprecht
the reason it called only one time , if you scroll it does not affect , can you update your questionHedve
don't forget to set maskToBounds on the CALayer to true if you want it to clip the layer's contents as well as sublayers.Selangor
R
2

actually there is a method for that state in UITableViewCell. it is layoutSubviews

-(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;
}
Romance answered 28/1, 2016 at 20:57 Comment(4)
what is _im ,is it view?Expectancy
it is a view that you want to round the corners.Romance
this is a method inside the customcell class( actually it is a UIView method, you need to override it inside CustomCell class). _im is a subview you added in a cell.Romance
Swift: let maskLayer = CAShapeLayer() maskLayer.path = UIBezierPath(roundedRect: someView.bounds, byRoundingCorners: [UIRectCorner.bottomLeft, UIRectCorner.bottomRight], cornerRadii: CGSize(width: 3.0, height: 3.0)).cgPath someView.layer.mask = maskLayerBackwards
D
1

UITableViewDelgate's

tableView:willDisplayCell:forRowAtIndexPath:

method will be called overtime when cell is about to be displayed on the screen. You can have your code in this method.

Delindadelineate answered 28/1, 2016 at 11:59 Comment(1)
Thanks for the info on that Delegate method I didn't know about. But unfortunately, it doesn't work for my case. It only does when I scroll the tableView, then it shows rounder corners, but not when table is loaded for the first timeRuprecht
O
1

The reason is that you put your code in wrong place. Method awakeFromNib is actually place where your views got initialized, and at this time _viewForTags.bounds gives you CGRectZero. You need to move your code into setSelected:animated: method, or give concrete CGRect value.

- (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;
}
Oraliaoralie answered 28/1, 2016 at 12:50 Comment(0)
O
0

Apply in 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;
Outwork answered 28/1, 2016 at 12:12 Comment(0)
P
0

This works with Swift 5 (and probably earlier):

    let maskLayer = CAShapeLayer()
    maskLayer.path = UIBezierPath(
                         roundedRect:       myView.bounds,
                         byRoundingCorners: [topLeft, .topRight],                             
                         cornerRadii:       CGSize(width:10.0, height:10.0)
                     ).cgPath
    myView.layer.mask = maskLayer
Pluckless answered 18/4, 2022 at 1:54 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.