UIView in cell - 无法绕右手弯角 [英] UIView in cell -- Can't round right-hand corners

查看:96
本文介绍了UIView in cell - 无法绕右手弯角的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在静态表格视图单元格中有一个矩形 UIView 对象。我有一个对象的出口,我试图有选择地圆角,但它不会在右边的圆角。这是故事板中的样子:

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:

我想配置的视图是浅黄色的。除了确认其他视图没有被剪裁之外,红色视图没有任何实际用途。

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.

没有约束问题或错误。以下是4S的外观:

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

我正在中进行配置tableView:willDisplayCell:forRowAtIndexPath:

使用此代码( myView 是上面淡黄色视图的出口):

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

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

我得到这个结果:

< img src =https://i.stack.imgur.com/JeBn8.pngalt =在此处输入图像说明>

所有4个角都是圆角。但我需要选择性地圆角,通常是顶角或底角。以下是舍入两个底角的代码:

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;

结果如下:

它不起作用对于任何一个右角。它适用于任何一个左角。

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

我在普通视图控制器上(在表视图之外)使用视图对象测试了bezier代码,代码按预期工作 - 我可以有选择地围绕任何角落。

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?

推荐答案

我认为这是因为您的视图的框架,因此在创建图层蒙版后边界会发生变化。

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

作为tableView的文档:willDisplayCell:forRowAtIndexPath:states:委托返回后,表视图仅设置alpha和frame属性,然后仅在行滑入或滑出时为行设置动画。

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.

最好的策略是继承UITableViewCell并在layoutSubviews中执行图层调整。

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

这篇关于UIView in cell - 无法绕右手弯角的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆