Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Round corners on UITableView

What is the best way to get round corners on an entire UITableView as seen in Stocks and Spotlight? The grouped style doesn't solve the problem because the round corners scroll away with the cell. I'm trying to clip the view so the corners are always round regardless of scroll position.

I saw another discussion about doing this to a UIImage that suggested masking it with another image. I'm not sure if this would work because I need taps to pass through to the table. This isn't isn't ideal for me because I want the background pattern to show through through the corners.

like image 567
Scrollwheelie Avatar asked Jul 09 '09 22:07

Scrollwheelie


People also ask

How do you round corners of cells in Swift?

To apply rounded corners, set a cornerRadius on both the layer and contentView. layer properties. On the contentView. layer , set masksToBounds to true so the contentView contents are clipped to the rounded corners.

How do you make a button corner rounded in WPF?

You can achieve rounded corners on a button without having to write any XAML (other than a Style, once) and without having to replace the template or set/change any other properties. Just use an EventSetter in your style for the button's "Loaded" event and change it in code-behind.


1 Answers

It's an old question but perhaps you still want to know how to do this.

I reproduced a tableView like in Stocks/Spotlight. The trick is

view.layer.cornerRadius = 10;  

For this to work you need to include the QuartzCore into the class that you call that property:

#import <QuartzCore/QuartzCore.h> 

I heard that this only works since OS 3.0. But since my application is using core data it wasn't a problem because it was already for OS 3.0 and hight.

I created a custom UIView with a subview with cornerRadius 10 and with

view.backgroundColor = [UIColor clearColor]; 

Then you have to place an UITableView grouped style in that subview. You need to set the backgroundColor to clearColor and the separatorColor to clearColor. Then you have to position the tableview inside the rounded corner view, this is done by setting the frame size and origin. My loadView class of my custom UIView looks like this:

self.view = [[UIView alloc] init]; self.view.backgroundColor = [UIColor clearColor];  CustomUIViewClass *scherm = [[CustomUIViewClass alloc] init];  CGRect frame; frame.origin.x = 10; frame.origin.y = 50; frame.size.width = 300; frame.size.height = 380;  scherm.frame = frame; scherm.clipsToBounds = YES; scherm.layer.cornerRadius = 10;  [self.view addSubview:scherm];  CustomUITableViewClass *table = [[CustomUITableViewClass alloc] initWithStyle:UITableViewStyleGrouped];  frame.origin.y = -10; frame.origin.x = -10; frame.size.width = 320; frame.size.height = 400;  table.tableView.frame = frame; [scherm addSubview:table.tableView]; 

I hope you understand my english, maybe I will write a short blog post about this technique with a sample project, will post the link here when I'm ready.

like image 113
HansPinckaers Avatar answered Oct 01 '22 18:10

HansPinckaers