Rounded corners in a UITableView (iOS7)

20,610

Solution 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.

Solution 2

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

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

Solution 3

Use below...

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

Solution 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.

Share:
20,610
joan
Author by

joan

iOS developer

Updated on January 11, 2020

Comments

  • joan
    joan over 4 years

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

    enter image description here