Background color for UISearchController in UITableView - ios

I have a SearchController's search bar inserted programatically into a UITableView's tableHeaderView. When I pull up to view the search bar or refresh the table, I get this weird darker gray that you can see in the following image, in between the refresher activity indicator and the search bar (this background color persists even when I remove the refresher view):
tableView.tableHeaderView = searchController.searchBar
I've tried changing this background color in every way I can think of:
tableView.backgroundColor = UIColor.redColor()
tableView.tableHeaderView!.backgroundColor = UIColor.redColor()
searchController.searchBar.backgroundColor = UIColor.redColor()
view.backgroundColor = UIColor.redColor()
Nothing works. This dark gray isn't one of the custom colors I use in my project so I know I didn't set it manually. As soon as I take out the searchController everything works just like before: that dark gray is replaced by the lighter gray seen everywhere else.

Set the UITableView's backgroundView to a new useless view:
self.tableView.backgroundView = [UIView new];
Seems illogical, but works like a charm :)

Cameron E's correct answer in Swift:
tableView.backgroundView = UIView()
Note that self.tableView.backgroundView = nil does not work.

Try this:
[searchController.searchBar setBarTintColor:[UIColor blackColor]];
[searchController.searchBar setTintColor:[UIColor whiteColor]];
Hope this helps.

Just set an empty view as the UITableView's backgroundview
[self.tableView setBackgroundView:[[UIView alloc] initWithFrame:CGRectZero]];

try to change color of
self.navigationController?.navigationBar = UIColor(red: 1, green: 1, blue: 1, alpha: 1)

Related

Why can't I set tableview background color to white?

Whenever I do and run the app it's light gray. I've changed EVERY color I can and white just does not stay white. Every other color I've tried works, just not white.
I tried these 2
self.tableView.backgroundView = UIView()
self.tableView.backgroundView!.backgroundColor = .white
self.tableView.backgroundColor = .white
And I've tried to just set it in storyboard to white
I had this issue for reasons I can't fathom when setting the TableView's View background colour to white or a light-grey colour in the XIB file. Any other colour worked just fine. Only fix I found was to set the background colour to white in the UIViewController's viewDidLoad function like so:
tableView.backgroundColor = UIColor.white
You should create the view with the correct rect:
self.tableView.backgroundView = UIView(frame: self.tableView.frame)

How to make UINavigationBar background transparent?

First of all,
I've seen all the answers at How to make UINavigationBar Transparent in IOS 8? Transparent UINavigationBar and Make UINavigationBar transparent.
They just don't seem to work for me.
My regular view controller (before trying to make the navigation bar transparent) doesn't have any issues:
I'm using (tried both in viewDidLoad and viewWillAppear:):
[self.navigationController.navigationBar setBackgroundImage:[UIImage new]
forBarMetrics:UIBarMetricsDefault];
self.navigationController.navigationBar.shadowImage = [UIImage new];
self.navigationController.navigationBar.translucent = YES;
self.navigationController.view.backgroundColor = [UIColor clearColor];
I'm getting this:
Gray status bar background, completely white navigation bar which doesn't blend with the status bar, and then the view starts. All the 'solutions' at the other questions' answers' yield the same result for me.
I've also tried setting self.edgesForExtendedLayout = UIRectEdgeNone; or self.edgesForExtendedLayout = UIRectEdgeAll; but that also didn't have any impact.
How can I make my navigation bar transparent without messing up everything?
UPDATE: Following Warif Akhand Rishi's answer, I've changed self.navigationController.view.backgroundColor = [UIColor clearColor]; to self.navigationController.navigationBar.backgroundColor = [UIColor clearColor];, now I'm getting a gray, unified status/navbar, but still not transparent:
UPDATE 2: I've hooked up the view debugger, and that gray background seems to come from deep down from the roots of view hierarchy, and my view's content is not extending up. I've tried self.edgesForExtendedLayout = UIRectEdgeAll; again with the latest code but still no avail:
swift 4 transparent nav bar:
(be sure view extends behind nav bar to show through, otherwise will just be black)
navigationController?.navigationBar.isTranslucent = true
navigationController?.navigationBar.setBackgroundImage(UIImage(), for: .default)
navigationController?.navigationBar.shadowImage = UIImage() //remove pesky 1 pixel line
or just match navbar color to color of your current vc, but keep it opaque. with translucent set to false child views will line up with navbar instead of going under it.
navigationController?.navigationBar.isTranslucent = false
navigationController?.navigationBar.barTintColor = UIColor.yourColor
navigationController?.navigationBar.shadowImage = UIImage() //remove pesky 1 pixel line
Change your
self.navigationController.view.backgroundColor = [UIColor clearColor];
to this
self.navigationController.navigationBar.backgroundColor = [UIColor clearColor];
Okay, after struggling, I've solved the problem on my own. There was more than one problem. It wasn't about the extended edges, it was about the line self.navigationController.view.backgroundColor = [UIColor clearColor]; (which had to be self.navigationController.navigationBar.backgroundColor = [UIColor clearColor]; as Warif Akhand Rishi suggested) and also my table view's clip subviews property. I've changed that line and also turned off clipping of my table view and now it works as expected.
For iOS 13 and the UINavigationBarAppearance API:
let navAppearance = UINavigationBarAppearance()
navAppearance.configureWithTransparentBackground()
self.navigationItem.standardAppearance = navAppearance
Eliminate 5+ lines of shadow/background/color code!
I'm a little late to the party, but I recently needed to do the same thing and I found the following actually works best (because it removes all shadows and bleed-throughs you may have from something lower in the stack):
guard let navBar = navigationController?.navigationBar else { return }
navBar.barStyle = .black
navBar.setBackgroundImage(UIImage(), for: .default)
navBar.shadowImage = UIImage()
navBar.isTranslucent = true
navBar.isHidden = false
1.Your NavigationBar is white,not black.So you must have a view (a white view) under NavigationBar,which is the superview of your greyView.The transparent setting works,but you cann't see it ,because the fontcolor is white too.
2.So you have to update your greyView's constraints,so it can extends under navigationbar .And then you can see your white title.
3.Maybe you have to Change your statusBar's UIStatusBarStyle to default or lightcontent,I noticed the font color of statusBar is white too.
The below code works for me
self.navigationController?.navigationBar.setBackgroundImage(UIImage(), for: UIBarMetrics.default)
self.navigationController?.navigationBar.shadowImage = UIImage()
self.navigationController?.navigationBar.isTranslucent = true
self.navigationController?.view.backgroundColor = .clear
self.navigationController?.navigationBar.backgroundColor = .clear

Can't change search bar tint color to be transparent in iOS 8

Upgraded from Xcode 5 to 6 and now my search bar tint is black.
Tried to change it through storyboard right pane > "Bar Tint" to clear color, but it's still black.
Also tried programmatically:
[self.searchBar setTintColor:[UIColor clearColor]];
Still black :(
Any ideas?
The tintColor property on search bars, much like UINavigationBar, changes the color of the buttons, as well as changes the color of the blinking cursor, not the actual search bar background. What you want to use is the barTintColor property.
searchbar.barTintColor = [UIColor orangeColor];
searchbar.tintColor = [UIColor greenColor];
Produces the following ugly, yet informative, result:
If you want to have a completely transparent search bar, you need to set the background image as well:
searchbar.barTintColor = [UIColor clearColor];
searchbar.backgroundImage = [UIImage new];
EDIT: I would strongly advise against traversing and modifying the subviews of any UIKit object, as has been proposed in other answers. From Apple's documentation:
For complex views declared in UIKit and other system frameworks, any
subviews of the view are generally considered private and subject to
change at any time. Therefore, you should not attempt to retrieve or
modify subviews for these types of system-supplied views. If you do,
your code may break during a future system update.
https://developer.apple.com/documentation/uikit/uiview/1622614-subviews
I got to change it on iOS 9 using Swift 2.0 this way:
let image = UIImage()
searchBar.setBackgroundImage(image, forBarPosition: .Any, barMetrics: .Default)
searchBar.scopeBarBackgroundImage = image
On Swift 3:
Setting up the background of the searchBar to an empty image:
searchBar.setBackgroundImage(image, for: .any, barMetrics: .default)
searchBar.scopeBarBackgroundImage = image
for programmatically change search bar tint color :
if let textfield = searchBar.value(forKey: "searchField") as? UITextField {
textfield.textColor = #colorLiteral(red: 0.3921568627, green: 0.3921568627, blue: 0.3921568627, alpha: 1)
textfield.backgroundColor = #colorLiteral(red: 1, green: 1, blue: 1, alpha: 1)
}
for storyboard :
I've been seeing this problem as well on iOS8 / XCode 6. I did not get the search bar translucent. searchbar.barTintColor or searchbar.tintColor setting to clear color did not help or shows a black searchbar.
The only workaround i found was to set a translucent background png image (alpha=0.0) and setting searchbar.barTintColor to clear color.

UISearchBar and/or UISearchDisplayController color

UPDATE: Thanks for all the answers. I would like to add that the search bar used is actually a UISearchBar embedded as part of UISearchDisplayController that is set to the a UITableView's header.
I've created a sample project exhibiting this behavior here: https://dl.dropboxusercontent.com/u/3497087/TestSearchDisplayController.zip. I tried setting the barTintColor to blue and black. The most obvious thing is that when setting to black, I get a grayish bar.
I appreciate all answers and ideas, thank you.
I am working on skinning the app that I'm currently working on, and I seem to hit a roadblock with UISearchBar and/or UISearchDisplayController bar color.
The first issue I have revolves around setting the barTintColor for UISearchBar that is attached as a tableview header. I've set it to blackColor in Interface Builder. However, when the app runs, the color doesn't seem to be black, but some sort of gray, with an ugly white line above! I've tried setting this thru code, but that doesn't seem to help too. See screenshots below.
My second question revolves around UISearchDisplayController. I wanted black color when the search display controller takes over the top of the screen. I've tried setting the color code, but the only color that it won't take is, again, black color!
[[UISearchBar appearance] setBackgroundImage:[UIImage imageNamed:#"black"]];
check out your sample project
You can try with this:
_searchBar.barTintColor = [UIColor blackColor]; // change the barColor
_searchBar.tintColor = [UIColor whiteColor]; // change the title color
It's as simple as setting searchBar.barTintColor to the color you want. No need for images. searchBar.tintColor changes the color of the Cancel button.
I just add a helper function to set everything, including the cancel button color (tint.color). This called in ViewDidLoad:
//Set the searchbar settings, delegates
func setSearchBar() {
searchController = UISearchController(searchResultsController: nil)
searchController.searchResultsUpdater = self
searchController.searchBar.placeholder = "Enter Search Text"
searchController.searchBar.barTintColor = UIColor(red: 72/255, green: 100/255, blue: 156/255, alpha: 1)
searchController.searchBar.tintColor = UIColor.white
searchController.hidesNavigationBarDuringPresentation = false
searchController.dimsBackgroundDuringPresentation = false
}
You can also set own color for searchbar color (barTintColor). This works in Xcode 8 and Swift 3.
For your first question, I had the same issue few months ago. Have a look at: iOS7, backgroundImage for UISearchBar
At the end, it uses the method:
setBackgroundImage:forBarPosition:barMetrics:
Getting control of the iOS 7 search bar background color is tricky. The easiest and most reliable approach: Make a solid black image and call setBackgroundImage:forBarPosition:barMetrics:. (Getting the second and third arguments right is also tricky!) Also explicitly set the bar's translucent to NO just to be on the safe side. Do all this in code; do not rely on Interface Builder to get things right.
For Background Color of Searchbar :
For TintColor:
[self.searchDisplayController.searchBar setTintColor:[UIColor redColor]];
Hi You can try with this
_searchBar.tintColor = [UIColor blackColor];
_searchBar.backgroundColor = [UIColor clearColor];
_searchBar.backgroundImage = nil;

Change background of a grouped UITableView

I'm having some trouble trying to change the background of a UITableView with groups.
_tableView.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageNamed:#"tableViewBg.png"]];
This usually works on every other UITableView, but not the one with groups, is there something else I have to do?
In IB I have the background color set to a clear color, but that doesn't do much.
You additionally need to disable the background view of _tableView:
[_tableView setBackgroundView:nil];
_tableView.backgroundColor = [UIColor redColor];
No need to add new view to backgroundView. This is working for me in iOS6.
why don't you set the tableView.backgroundView? you can alloc an image view withe the specified image and pass it to the background view instead of setting the background color.
What I usually do with grouped UITableViews is set the background color to clear, and the set that pattern image to the parents view.
self.parentViewController.view.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageNamed:#"tableBG.png"]];
self.tableView.backgroundColor = [UIColor clearColor];
tableView.backgroundView = nil;
tableView.backgroundColor = [UIColor colorWithPatternImage: xxx];
// tableView.backgroundColor = [UIColor redColor]; // is ok
if you set set the backgroundColor as this, when you scroll the tableView, the backgroundColor view will scroll also. so, you can: tableView.backgroundView = nil; self.view.backgroundColor = ...
The selected answer works but it clears the background of both tableHeaderView and table section header view. If you just want table header to be of a certain color say white and section header still to be default grey than do the following -
tableView.tableHeaderView.inputView.backgroundColor = [UIColor whiteColor];
Just want to add to Nirav's answer - it can also be done using the iOS 5 appearance proxy.
[[UITableView appearance] setBackgroundView:nil];
[[UITableView appearance] setBackgroundColor:[UIColor lightGreyColor]];
The advantage is that it is applies globally, so you can group all your UI customisations in one place. However, it will apply to all tableViews (not just grouped style).
For iOS 9+, changing the background color of the UITableView is enough.
Objective-C
tableView.backgroundColor = [UIColor redColor];
Swift
tableView.backgroundColor = .red
Swift 4.2
TO REMOVE BACKGROUND VIEW & COLOR
tableView.backgroundView = nil
tableView.backgroundColor = .clear

Resources