问题
In iOS7, how can I draw cell's rounded corners in a UITableView? See an example:
回答1:
Your UITableview contains 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
//If iOS version < 10
For 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
For 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 framework explicitly.
回答2:
I subclassed UITableViewCell and had to leave out contentView to make it work.
cell.layer.cornerRadius = 10
cell.layer.maskToBounds = true
回答3:
Use below...
[cell.contentView.layer setCornerRadius:7.0f];
[cell.contentView.layer setMasksToBounds:YES];
回答4:
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/NIB file, add a UIView with keeping top, bottom, left & right corners margin via constraints, keep cell background transparent and round UIView corners. My solution was good at that situation. But Always go for best practices.
来源:https://stackoverflow.com/questions/22049226/rounded-corners-in-a-uitableview-ios7