[英]How to corner radius only top left and top right of table view custom cell on Iphone +
How to corner radius only top left and top right of table view custom cell .I write following code and it working fine on iphone 6, 5,7 . 如何只在表格视图自定义单元格的左上角和右上角半径。我编写以下代码,它在iphone 6,5,7上工作正常。 but on Iphone 6+, 7+ it look like given screen.
但是在Iphone 6 +,7 +上,它看起来像给定的屏幕。
My code is ---- 我的代码是----
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
CustomeCell *cell = [tableView dequeueReusableCellWithIdentifier:@"CustomeCell"];
if (indexPath.row == 0) {
UIBezierPath *maskPath = [UIBezierPath
bezierPathWithRoundedRect:cell.contantsView.bounds
byRoundingCorners:(UIRectCornerTopLeft | UIRectCornerTopRight)
cornerRadii:CGSizeMake(4, 4)
];
CAShapeLayer *maskLayer = [CAShapeLayer layer];
maskLayer.frame = cell.bounds;
maskLayer.path = maskPath.CGPath;
cell.contantsView.layer.mask = maskLayer;
cell.contantsView.layer.masksToBounds = YES;
}
return cell;
}
And my screen look like --: 我的屏幕看起来像-:
it this screen I corner radius of only first and last cell . 在此屏幕上,我的拐角半径只有第一个和最后一个单元。 Center cell are look fine .
中心单元看起来很好。 I want to look all cells width same .
我想使所有单元格的宽度相同。
The issue is that when cellForRowAtIndexPath
is called, the cell.contentView.bounds
(and I assume your contantsView
is inside that) is not the same as the cell.bounds
. 问题是,当
cellForRowAtIndexPath
被调用时, cell.contentView.bounds
(我假设你contantsView
是内部的)是不一样的cell.bounds
。 While you're setting the frame
of your layer using the bounds
of the contantsView
, but when creating the UIBezierPath
for your mask, you are using the bounds
of the cell
, itself. 当你设置
frame
使用您层bounds
的的contantsView
,但在创建时UIBezierPath
为你的面具,你正在使用的bounds
的的cell
,本身。 These are not the same (at the time of cellForRowAtIndexPath
, at least). 它们是不同的(至少在
cellForRowAtIndexPath
时)。
So, that suggests a couple of possible fixes: 因此,这建议了一些可能的解决方法:
The simple fix is to cell.bounds
rather than cell.contantsView.bounds
when defining the bezier path. 定义
cell.contantsView.bounds
路径时,简单的解决方法是对cell.bounds
而不是cell.contantsView.bounds
。 You're using one CGRect
for the path
of the shape layer and another for the frame
of that same shape layer. 您将一个
CGRect
用于形状层的path
,将另一个CGRect
用于相同形状层的frame
。 You really should use the same CGRect
for both, and cell.bounds
doesn't suffer this weird size issue. 您确实应该对两者都使用相同的
CGRect
,并且cell.bounds
不会遇到这个奇怪的大小问题。
The frame
of the contentView
of the cell changes after cellForRowAtIndexPath
is called. 调用
cellForRowAtIndexPath
之后,单元格的contentView
的frame
更改。 So the above fix just happens to work because cell.bounds
doesn't change, whereas the contentView
(and thus likely your contantsView
) does. 因此,上面的修复恰好起作用了,因为
cell.bounds
不变,而contentView
(因此可能是您的contantsView
)发生了变化。
The correct fix is that you really should put the masking of the layer in the layoutSubviews
for that view that you're masking. 正确的解决方法是,您确实应该将该图层的遮罩放入要遮罩的该视图的
layoutSubviews
中。 (This, by the way, will also make sure that the mask gets set appropriately if you change orientation on the device, as well as anything else that might change the dimensions of the contentView
.) (顺便说一下,如果您更改设备的方向以及其他可能更改
contentView
尺寸的方向,这还将确保正确设置了遮罩。)
If you don't want to bother subclassing the contantsView
and implementing layoutSubviews
for that, you can, theoretically, put the masked background color on the cell itself, rather than on contantsView
, and implement layoutSubviews
in CustomeCell
. 如果你不希望打扰子类
contantsView
和实施layoutSubviews
的是,你可以,理论上,把蒙面背景颜色对细胞本身,而不是contantsView
,并实现layoutSubviews
在CustomeCell
。 (Frankly, that might be the right view to be applying this background to, anyway.) But at whatever view you apply the background color, the masking of the view's corners should really be happening in its layoutSubviews
. (坦率地说,无论如何,应用此背景可能都是正确的视图。)但是,无论您应用哪种背景颜色,都必须在其
layoutSubviews
真正遮盖该视图的角。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.