Reputation: 2531
In iOS7, how can I draw cell's rounded corners in a UITableView? See an example:
Upvotes: 31
Views: 20983
Reputation: 15015
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.
Upvotes: 33
Reputation: 3708
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.
Upvotes: 3
Reputation: 2697
I subclassed UITableViewCell and had to leave out contentView to make it work.
cell.layer.cornerRadius = 10
cell.layer.maskToBounds = true
Upvotes: 13
Reputation: 31637
Use below...
[cell.contentView.layer setCornerRadius:7.0f];
[cell.contentView.layer setMasksToBounds:YES];
Upvotes: 3