iOS SearchDisplayController prevents tableview from background color change (background stays gray)
When using a UISearchDisplayController
in a UITableViewController
, 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 a UIStoryboard
), you are actually added another UITableView
that, when activated, must be managed by code in your UITableViewController
file in the same manner as any other UITableView
.
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 search UITableView
background colour to display a black colour, I can suggest this code (works in a TVC in my app)...
- (void)viewDidLoad {
[super viewDidLoad];
[self.tableView setBackgroundView:nil];
[self.tableView setBackgroundColor:[UIColor blackColor]];
[self.searchDisplayController.searchResultsTableView setBackgroundView:nil];
[self.searchDisplayController.searchResultsTableView setBackgroundColor:[UIColor blackColor]];
}
...
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 a UITableViewController
, 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 the UISearchBar
as an outlet for the UISearchDisplayController
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:
- (void)viewDidLoad {
[super viewDidLoad];
...<other code>...
CGRect frame = self.tableView.bounds;
frame.origin.y = -frame.size.height;
UIView* viewBackgroundBlack = [[UIView alloc] initWithFrame:frame];
[viewBackgroundBlack setBackgroundColor:[UIColor blackColor]];
[self.tableView addSubview:viewBackgroundBlack];
[self.tableView setBackgroundView:nil];
[self.tableView setBackgroundColor:[UIColor blackColor]];
[self.searchDisplayController.searchResultsTableView setBackgroundView:nil];
[self.searchDisplayController.searchResultsTableView setBackgroundColor:[UIColor blackColor]];
}
Different background colors for the top and bottom of a UITableView
The easiest and most lightweight way to solve this problem is:
- Set the background color of the table view to whatever you want - in your case, white.
- Put the search bar view inside a container view. Set the table view's header view to this container view (instead of the search bar view itself, which is probably what you were doing previously).
- In that container view, add another subview with frame equal to a rect like (0, -480, 320, 480), and set the background color of that subview to whatever color you want - in your case, grayish.
That should be all you need to do. I just did this myself and achieved the look I wanted, exactly the same as the Mail app. Using scrollViewDidScroll is a major waste of CPU resources, and subclassing UITableView is super messy, IMO.
UISearchDisplayController with ViewController issues
- You have to set
UIView
background color to black (not justUITableView
color). - You have to position your table view below
UISearchBar
, you can do this either programmatically or via IB. I recommend second option. Pretty easy.
tableView: didSelectRowAtIndexPath: looks awful if not setting the next viewcontroller's background color
The default background colour for your new viewController is clear, so when you do an animated push you can see through the new viewController temporarily as it pushes itself on top of the navigation stack.
Setting an opaque background colour will mean that your new VC is not transparent, and thus you won't be able to see through it to the VC underneath.
However, I'd recommend you set your backgroundColor
in viewDidLoad
of your new viewcontroller, instead of in the one that's doing the presenting (it's not really the responsibility of your presenting viewController to do that).
Search Bar background color Gray ios7
Try this:
if(IOS_7)
{
self.searchBar.searchBarStyle = UISearchBarStyleMinimal;
}
UIViewController with UITableView and UISearchBar gap issue
The gap is occurring when setting UINavigationBar
translucent property to NO
. Setting it to YES
resolve the issue. Animation is smooth again and there's no gap between UISearchBar
and UITableView
. I can't explain why, and it appears to be a bug on Apple side as it is easy to reproduce by creating a sample project.
Light gray background in bounce area of a UITableView
As of iOS 7, you can tinker this by changing the tableview background view.
[self.tableView setBackgroundView:view];
make the view's background colour the same as your parent view colour.
Related Topics
Is the Function 'Dlopen()' Private API
Coreanimation - Opacity Fade in and Out Animation Not Working
How to Upload Video to Server from Iphone
Change the Alpha Value of the Navigation Bar
Will Appstore Reviewers Allow Us to Use Dynamic Library in iOS8
How to Receive Push Notifications of an Other App
Collectionview with the Horizontal Scroll with Mulitple Section
Xcode 11.4. Navigation's Title Color Gone Black from Storyboard
How to Animate a Uiview with Constraints in Swift
It Gives Errors When Using Swift Static Library with Objective-C Project
Xcode 8 with Mixed Swift and Objective-C Project Generated "Modulename-Swift.H" Header Not Found
Unrecognized Selector Sent to Instance While Archiving Data (Nscoding)
How to Represent Core Data Optional Scalars (Bool/Int/Double/Float) in Swift