joan
joan

Reputation: 2531

Rounded corners in a UITableView (iOS7)

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

enter image description here

Upvotes: 31

Views: 20983

Answers (4)

Hussain Shabbir
Hussain Shabbir

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

Abhishek Thapliyal
Abhishek Thapliyal

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

hidden-username
hidden-username

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

Fahim Parkar
Fahim Parkar

Reputation: 31637

Use below...

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

Upvotes: 3

Related Questions