Light gray background in "bounce area" of a UITableView

Viewed 38009

Apple's iPhone apps such as Music and Contants use a search bar in a UITableView. When you scroll down so that the search bar moves down, the empty space above the scroll view's contents has a light gray background color (see screenshot).

Screenshot of Contacts app showing light gray background

(Notice that the search bar has a slightly darker edge line at its top. This is not there for the default UISearchBar, but subclassing should take care of that.)

I tried setting the background color of the UITableView, but that affects the rows as well. Does anyone know how to achieve this effect? Am I going to have to override implement drawRect: or is there a built in way?

16 Answers

I've only found one way to do this. You have to set the backgroundColor of the UITableView to be transparent, set the backgroundColor of the cell's contentView to whatever colour you want the actual cells to be, then crucially you have to get the light grey colour to appear behind the UITableView. That last step you can do by either setting the backgroundColour of the UIWindow, or of whatever is containing or your tableViewController.

So, assuming you have a view controller that is derived from UITableViewController, insert these lines in the -(void)viewDidLoad method:-

// sets the background of the table to be transparent
self.tableView.backgroundColor = [UIColor colorWithWhite:1.0 alpha:0.0];
// assuming we are inside a navigation or tab controller, set the background
self.parentViewController.view.backgroundColor = [UIColor lightGrayColor];

Then inside the part of tableView:cellForRowAtIndexPath: that creates new cells, add:-

// set an opaque background for the cells 
cell.contentView.backgroundColor = [UIColor whiteColor];

I don't think you want to override drawRect. Most likely what you're seeing is the background colour of another view or the window, which lies "behind" (i.e. is a superview of) the table view. There's usually a fairly complex layers of UIViews in Apple's UI widgets. Explore the view hierarchy in GDB, look at [myView superview] and then [someSuperView subviews] and try manipulating their BG colours in the debugger to see if you can find which one it is. However, if you implement a fix this way, be warned that it may not be future compatible.

You might also try setting the BG colour of one of the views behind the tableview in Interface Builder (or of the window itself).

If you are using a tableviewcell, you can set the view background to be opaque white. Then use

self.tableView.backgroundColor = [UIColor grayColor];

in the view did load method.

I'm sure that that is [UITableView backgroundColor]. You have affected rows, because rows have backgroundColor == clear (or semi-transparent). So, If you'll make rows non-trasparent, all will work fine. This will be solution.

I followed the tip outlined by Peylow, for a UITableView, by simply adding a subview. My only change from the code was to grab a color a bit closer to the one used in Apple apps, plus I got it a bit closer to Apple's look of having a line above the UISearchbar by reducing the frame origin y coordinate by one pixel:

frame.origin.y = -frame.size.height - 1

In my case the solution was to create a headerview for the table and assign a color, it solved the black background in bounce area in my apps when in dark mode. I did the same to its tableFooterView.

 table.tableHeaderView = UIView()
 table.tableHeaderView!.backgroundColor = UIColor.white
    
Related