This might be a bug in iOS 7:
I used storyboard to drag in Search Bar and Search Display Controller in my tableview controller. The background color of my tableview is altered after that.
I am using an example from AppCoda's "how-to-add-search-bar-uitableview" to demo this issue. I only changed the example code with 1-line add-on in ViewDidLoad
:
[_tableView setBackgroundColor:[UIColor blackColor]];
See my screenshots here:
The bottom of the tableview has already changed to black:
But the top stays as gray though tableview's background was set to black: This only happens when I drag in "Search Bar and Search Display Controller". If I remove the "Search Display Controller" from storyboard, it's all good.
The problem arise when
UISearchBar
set as header view from Storyboard. WrappingUISearchBar
inUIView
seems to solve the problem but brings other problems regarding compatibility withUISearchDisplayController
.I hacked into UI and basically found that
UITableView
positions a stub view between its top edge and header view and it's the first subview inUITableView
hierarchy. Changing its background color won't work because it's updated on each scroll position change.So my solution is to simply zero the
alpha
value for it. Therefore we delegate the background color toUITableView
itself. It works perfectly fine on iOS 7.1 and seems to be a light-weight patch in this situation.When using a
UISearchDisplayController
in aUITableViewController
, it is very important to remember that you are dealing with two table views.So when you drag a "Search Bar and Search Display Controller" into a
UITableView
(and assuming you are doing this drag in aUIStoryboard
), you are actually added anotherUITableView
that, when activated, must be managed by code in yourUITableViewController
file in the same manner as any otherUITableView
.Consider this:
The UITableView that represents the complete data set can be called using
self.tableView
(or as you have written, the synthesised_tableView
).The UITableView that represents the filtered data set (filtered using your search criteria) can be called using
self.searchDisplayController.searchResultsTableView
If you'd like both your default
UITableView
and searchUITableView
background colour to display a black colour, I can suggest this code (works in a TVC in my app)......
UPDATE
Now that I have finished my long-winded lecture, I agree that there is in fact a "bug" of sorts in Xcode. If you delete the original
UISearchBar
and then add a new one to aUITableViewController
, before you connect it, do a Build & Run. A black background is visible below and above the table view. It is only after you set theUISearchBar
as an outlet for theUISearchDisplayController
that the black background is "replaced" with a grey background.So the solution...
With thanks to Olof's answer to Different background colors for the top and bottom of a UITableView.
REPLACE the code I have written above with this code, at the end of your
viewDidLoad
method:I tried to use:
self.tableView.backgroundView = [UIView new];
. It worked!