Reputation: 1173
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
?
Upvotes: 6
Views: 1506
Reputation: 12615
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
Upvotes: 0
Reputation: 1885
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;
}
Upvotes: 2
Reputation: 2281
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;
}
Upvotes: 1
Reputation: 9898
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;
Upvotes: 0
Reputation: 3956
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.
Upvotes: 1