objective-c UITableView (iOS7) 中的圆角

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/22049226/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-03 21:12:08  来源:igfitidea点击:

Rounded corners in a UITableView (iOS7)

objective-cuitableviewios7rounded-corners

提问by joan

In iOS7, how can I draw cell's rounded corners in a UITableView? See an example:

在 iOS7 中,如何在 UITableView 中绘制单元格的圆角?看一个例子:

enter image description here

在此处输入图片说明

回答by Hussain Shabbir

Your UITableviewcontains UIView, so just use this below lines of code for making it rounded corners. Also write this below line of code inside your tableview methods

您的UITableviewcontains UIView,因此只需使用下面的代码行使其成为圆角。也在你的 tableview 方法中写下这行代码

//If iOS version < 10

//如果iOS版本<10

For Objective-C:

对于Objective-C:

cell.contentView.layer.cornerRadius = 5;
cell.contentView.layer.masksToBounds = YES;

For Swift:

对于斯威夫特:

cell.contentView.layer.cornerRadius = 5
cell.contentView.layer.masksToBounds = true

//If iOS version >= 10

//如果iOS版本>=10

For Objective-C:

对于Objective-C:

cell.layer.cornerRadius = 5;
cell.layer.masksToBounds = YES;

For Swift:

对于斯威夫特:

cell.layer.cornerRadius = 5
cell.layer.masksToBounds = true

Note: No need to import QuartzCore frameworkexplicitly.

注意:无需QuartzCore framework显式导入。

回答by hidden-username

I subclassed UITableViewCell and had to leave out contentView to make it work.

我对 UITableViewCell 进行了子类化,并且不得不忽略 contentView 以使其工作。

cell.layer.cornerRadius = 10 cell.layer.maskToBounds = true

cell.layer.cornerRadius = 10 cell.layer.maskToBounds = true

回答by Abhishek Thapliyal

try table view delegate

尝试表视图委托

- (void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath
{
    cell.layer.cornerRadius = 10;
    cell.layer.masksToBounds = YES;
}

Note/Recommend: Better use Cell XIB/NIBfile, add a UIViewwith keeping top, bottom, left & rightcorners margin via constraints, keep cell background transparent and round UIViewcorners. My solution was good at that situation. But Always go for best practices.

注意/推荐:更好地使用 Cell XIB/NIB文件,添加一个UIView,通过约束保持上、下、左、右角边距,保持单元格背景透明和圆形UIView角。我的解决方案很适合这种情况。但始终追求最佳实践。

回答by Fahim Parkar

Use below...

下面使用...

[cell.contentView.layer setCornerRadius:7.0f];
[cell.contentView.layer setMasksToBounds:YES];