简体   繁体   English

如何在Iphone +上仅在表格视图自定义单元的左上角和右上角半径

[英]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之后,单元格的contentViewframe更改。 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 ,并实现layoutSubviewsCustomeCell (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.

相关问题 为什么自定义视图的角半径仅应用于左上角和右上角? - Why is corner radius applied only to top left and right corner for a custom view? 如何在TableView自定义单元格上为UIView的左上角和右上角设置拐角半径? - how to set corner radius for top left and top right of a UIView on tableview Custom Cell? 如何在 UIView 中为左上角和右上角的圆角半径仅设置顶部、左侧和右侧的边框? - how to set boarder for only top, left and right with corner radius with top-left and top-right in UIView? 拐角半径仅适用于视图的左上角和左下角 - Corner radius only for top and bottom left corner of a view 右上角和左上角半径无法正常工作 - Top right and top left corner radius is not working properly 如何仅创建具有左上角和左下角半径的圆角UIButton - How to create rounded UIButton with top-left & bottom-left corner radius only 自定义表格视图的左上角和右上角 header - Custom top left and top right corner of a tableview header Flutter 仅顶部、右侧、左侧边框,右上角和左上角为圆角 - Flutter only top, right, left border with rounded top right and top left corner 如何仅为 UIView 的左上角和右上角设置cornerRadius? - How to set cornerRadius for only top-left and top-right corner of a UIView? 表格视图单元的角半径 - Corner radius for table view cell
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM