如何设置一个圆角半径为自定义单元格

问题描述:

enter image description here如何设置一个圆角半径为自定义单元格

这里是我的示例输出,在下面详细介绍有三种细胞,我已经设置圆角半径为每个自定义cells.but它不能正常工作.The对于第一小区拐角半径不工作,对右上和左上工作第二单元,请说什么这个

-(void)layoutSubviews 
    { 
[super layoutSubviews]; 
self.backgroundColor = [UIColor clearColor]; 
self.contentView.layer.cornerRadius =5; 
self.contentView.layer.masksToBounds = YES; 

} 

做我申请这上面的代码到我的手机类文件

+0

参阅此http://*.com/questions/18095223/changing-corner-radius-of-uitableview-grouped-in-ios6 – Signare

+0

感谢@Signare ,我会试试这个 –

尝试这个代码在awakeFromNib:()

self.backgroundColor = [UIColor clearColor]; 
self.contentView.layer.cornerRadius =5; 
self.contentView.layer.masksToBounds = YES; 
+0

不,它不工作,如果应用在awakeFromNib –

+1

然后你可以尝试在表视图委托方法tableView:willDisplayCell:forRowAtIndexPath:方法cell.backgroundColor = [UIColor clearColor]; cell.contentView.layer.cornerRadius = 5; cell.contentView.layer.masksToBounds = YES; –

+0

thanx对我来说@ kishan – iMHitesh

这里有一个不同的方法:在你的cellForRow刚刚回来,你可以试试下面的代码的单元格前:

CGFloat corner = 20.0f; 
UIBezierPath *path = [UIBezierPath bezierPathWithRoundedRect:cell.backgroundView.bounds byRoundingCorners:UIRectCornerAllCorners cornerRadii:CGSizeMake(corner, corner)]; 
CAShapeLayer *shapeLayer = (CAShapeLayer *)cell.backgroundView.layer; 
shapeLayer.path = path.CGPath; 
shapeLayer.fillColor = cell.textLabel.backgroundColor.CGColor; 
shapeLayer.strokeColor = [UIColor lightGrayColor].CGColor; 
shapeLayer.lineWidth = 1.0f; 

让我知道!

+0

同样的答案是有的http://*.com/questions/18095223/changing-corner-radius-of-uitableview-grouped-in-ios6。所以只是给链接。 – Signare

+0

@Signare哦你已经在答案中评论了这个链接。对不起这是我的错 ! –

在使用的cellForRowAtIndexPath

cell.layer.cornerRadius=5 

如果你有不同的细胞中不同的部分,然后确保该代码可用于所有部分

其他简单的选择是在故事板或XIB设置RuntimeAttributs写的。

首先选择你的观点(点击你的看法)>去Identiti督察(近属性检查器在您的Xcode右侧)>用户自定义运行属性> +单击并在此字段中添加以下两个一接一个。

layer.cornerRadius 
layer.masksToBounds 

清理并生成项目。不需要编写任何.h或.m文件。

对我来说,它是完美的作品,也许我的代码有助于某人。

NSInteger rowsAmount = [tableView numberOfRowsInSection:indexPath.row]; 如果(rowsAmount){

UIBezierPath *maskPath = [UIBezierPath bezierPathWithRoundedRect:cell.bounds byRoundingCorners:(UIRectCornerBottomLeft | UIRectCornerBottomRight | UIRectCornerTopLeft | UIRectCornerTopRight) cornerRadii:CGSizeMake(10.0, 10.0)]; 

CAShapeLayer *maskLayer = [[CAShapeLayer alloc] init]; 
maskLayer.frame = self.view.bounds; 
maskLayer.path = maskPath.CGPath; 

cell.layer.mask = maskLayer; 

}