iPad table backgroundView

How grey is your table background?

There were some subtle changes introduced to UITableView with iPhone OS 3.2 that change the way the table background is handled. The most obvious change is if you are using a grouped table view with the default background set to groupTableViewBackgroundColor. On the iPhone you get the familiar stripy grey background shown in the first image below. On the iPad you get a solid grey background as shown in the second image:

iPhone group table view iPad group table view

So far this appears to be only a cosmetic change but in fact the change is more than just a different default colour.

UITableView backgroundView

A new property was added to UITableView with the iPad and iOS 3.2. The property named backgroundView is defined as follows:

@property (nonatomic, readwrite, retain) UIView *backgroundView;

The description for this property in the UITableView class reference documentation is as follows: > A table view’s background view is automatically resized to match the size of the table view. This view is placed as a subview of the table view behind all cells , header views, and footer views.

So on the iPad the issue is not that Apple has changed the system value for the group table view background colour but that they have also introduced a new view that sits behind the table components. This is pretty useful if you want to use a custom view as the table background but it does break some old table tricks. For example, you might have something similar to the following in a viewDidLoad method of a table view controller:

UITableView *tableView = [self tableView];
tableView.backgroundColor = [UIColor clearColor];

The backgroundColor property is a property inherited from UIView, setting it to be clearColor allows the window/view behind the table view to show through. This works fine on the iPhone but will no longer work on the iPad because the backgroundView is in the way. As a workaround you can remove the new view by setting the property to nil:

tableView.backgroundView = nil;

Backwards Compatibility

Since the backgroundView property is new with iOS 3.2 you need to be careful using it in universal applications that must also run on earlier pre iOS 3.2 releases. One way to do that is to first check if the UITableView class responds to backgroundView:

if ([UITableView instancesRespondToSelector:@selector(backgroundView)]) {
  tableView.backgroundView = nil;
}

Never miss a post!

iOS Size Classes Cheat Sheet

Subscribe and get my free iOS Size Classes Cheat Sheet

Success! Now check your email to confirm your subscription and download your free guide to iOS Size Classes.

There was an error submitting your subscription. Please try again.

Unsubscribe at any time.
No time to watch WWDC videos?

Sign up to get my iOS posts direct to your inbox and I will send you a free PDF of my iOS Size Classes Cheat Sheet.

OK! Check your inbox (or spam folder) for an email to confirm your details and download your free guide to iOS Size Classes.

There was an error submitting your subscription. Please try again.

Unsubscribe at any time.
Archives Categories