UIView in cell — Can't round right-hand corner

2019-03-02 16:44发布

I have a rectangular UIView object in a static table view cell. I have an outlet to the object and I'm trying to selectively round corners but it will not round corners on the right. Here's what it looks like in the storyboard:

enter image description here

The view I want to configure is the pale yellow one. The red view serves no real purpose other than confirming that the other view is not being clipped.

There are no constraint issues or errors. Here's how it looks on a 4S:

enter image description here

I'm doing the configuration in tableView:willDisplayCell:forRowAtIndexPath:

With this code (myView is the outlet to the pale yellow view above):

self.myView.layer.cornerRadius = 5.0f;
self.myView.clipsToBounds = YES;

I get this result:

enter image description here

All 4 corners rounded. But I need to selectively round corners, normally both top or both bottom corners. Here is the code for rounding the two bottom corners:

CAShapeLayer *shape = [[CAShapeLayer alloc] init];
shape.path = [UIBezierPath bezierPathWithRoundedRect:CGRectMake(0, 0, self.myView.bounds.size.width, self.myView.bounds.size.height)
                                   byRoundingCorners:UIRectCornerBottomLeft | UIRectCornerBottomRight
                                         cornerRadii:CGSizeMake(5.0f, 5.0f)].CGPath;
self.myView.layer.mask = shape;
self.myView.layer.masksToBounds = YES;

And here is the result:

enter image description here

It doesn't work for either of the right-hand corners. It works fine for either of the left-hand corners.

I tested the bezier code with a view object on an ordinary view controller (outside of a table view) and the code works as expected--I can round any corner selectively.

Any ideas why it's not working in a table view cell?

How can I accomplish this without resorting to drawing code?

4条回答
聊天终结者
2楼-- · 2019-03-02 16:48

Give a try with this snippet:

CAShapeLayer *shape = [CAShapeLayer layer];
// Create the path (with only the top-left corner rounded)
UIBezierPath *maskPath = [UIBezierPath bezierPathWithRoundedRect:self.myView.bounds.bounds 
                                               byRoundingCorners:byRoundingCorners:UIRectCornerBottomLeft|UIRectCornerBottomRight
                                                     cornerRadii:CGSizeMake(5.0f, 5.0f)];

shape.frame = self.myView.bounds;
shape.path = maskPath.CGPath;

// Set the newly created shape layer as the mask for the image view's layer
self.myView.layer.mask = shape;

Layer masks will not render when used in combination with the CALayer renderInContext method. If you need to use this try rounding corners with the following: Just two rounded corners?.

查看更多
甜甜的少女心
3楼-- · 2019-03-02 16:54

Add [self.myView layoutIfNeeded];

Example:

[self.myView layoutIfNeeded];

CAShapeLayer *shape = [[CAShapeLayer alloc] init];
shape.path = [UIBezierPath bezierPathWithRoundedRect:CGRectMake(0, 0, self.myView.bounds.size.width, self.myView.bounds.size.height)
                                   byRoundingCorners:UIRectCornerBottomLeft | UIRectCornerBottomRight
                                         cornerRadii:CGSizeMake(5.0f, 5.0f)].CGPath;
self.myView.layer.mask = shape;
self.myView.layer.masksToBounds = YES;
查看更多
爷的心禁止访问
4楼-- · 2019-03-02 16:56

Try adding this

shape.path = [UIBezierPath bezierPathWithRoundedRect:self.myView.bounds
                                   byRoundingCorners:UIRectCornerBottomRight
                                         cornerRadii:CGSizeMake(5.0f, 5.0f)].CGPath;

and this

shape.frame = self.myView.bounds; //Right before setting your self.mView.layer.mask = shape;
查看更多
虎瘦雄心在
5楼-- · 2019-03-02 16:59

I think it happens because of your view's frame and therefore bounds get changed after you make a layer mask.

As documentation for tableView:willDisplayCell:forRowAtIndexPath: states: After the delegate returns, the table view sets only the alpha and frame properties, and then only when animating rows as they slide in or out.

The best tactics would be subclassing UITableViewCell and perform layer adjustments in layoutSubviews.

查看更多
登录 后发表回答