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;
Related
I have set a color of my Navigation bar in Appdelegate so my app uses same color for all the navigation appearing in different screens. But Now for one or two controllers I want to change the navigation color. I used this code in viewDidLoad of specific controller but It isn't working
self.navigationController?.navigationBar.backgroundColor = UIColor.whiteColor()
I have tried this code in viewWillAppear function too but still it doesn't work
The property you are looking for is not backgroundColor, but rather barTintColor.
self.navigationController?.navigationBar.barTintColor = UIColor.whiteColor()
Please use tint color
[nav.navigationBar setBarTintColor:[UIColor blueColor]];
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)
guys, I am trying to customize UITabBar, but problem is that when I set up simple green background color in UITabBar.background, result is insufficient. Like something lies on background. Here it is look like:
UITabBarController is created in storyboard, but background color I set up in code. Here is appDelegate:
TabBarController* bc=(TabBarController*)self.window.rootViewController;
bc.tabBar.backgroundColor=[UIColor greenColor];
If I set up pictures on background and on items - they just almost invisible. I've tried playing with tint colors on the right bar in storyboard, but all was useless.
I had a similar issue when programmatically creating the UITabBarController background color in iOS 8. The problem was I was using backgroundColor when I should have been be using barTintColor. Try this.
TabBarController* bc=(TabBarController*)self.window.rootViewController;
bc.tabBar.barTintColor=[UIColor greenColor];
Let me know if that works for you.
You need to set the transparency of the tabBar:
Swift :
self.tabBar.translucent = false
Objective C :
[[self tabBar] setTranslucent:NO]
The same in Swift, iOS 9.
self.tabBarController?.tabBar.barTintColor = UIColor(red: 254/255.0, green: 200/255.0, blue: 93/255.0, alpha: 1.00)
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.
I'm developing a small collectionview 'framework' to behave like a browser tab bar (think chrome) on the iPad. The code is all done, custom flow layout, reordering, and so on and is organized as so :
• TabBarCollectionViewController .h/.m/.xib contains the high logic of the collection view (delegates + datasource methods). I have the xib to configure the collectionView settings and set the custom flow layout (I could do this programmatically, but oh well it's easier that way).
• CustomFlowLayout .h/.m (subclass of flow layout)
• TabBarCell .h/.m/.xib (subclass of collectionviewcell)
Then I'm adding the TabBarCVC as a childViewController on my main viewController (this viewController has many childViewController and subviews) and then as a subview.
At this point all is working fiiiiine.
Now the problem, it's so stupid i can't believe i haven't found a way to do this, the backgroundColor of the collectionView is not settable to clearColor. I can put it in gray or whatever color, but that it doesn't support transparency. The cell background color is also clear and does work.
I need the collectionView to be transparent to show the texture on the main view behind. Any insight would be much appreciated, or perhaps i'll fill my first radar to apple.
If i can't find any solution i'll just add the 'screenshot' of the texture supposed to be behind the collectionView and add it as a imageView in the collectionView's backgroundView.
In my case I had the background color in the storyboard set to Default. This caused it to have a black background. Changing it to Clear Color worked.
Try setting the color to clear and the background view to an empty view like so...
self.collectionView.backgroundColor = [UIColor clearColor];
self.collectionView.backgroundView = [[UIView alloc] initWithFrame:CGRectZero];
Ok so i'm feeling pretty stupid now. I left an empty UIView behind, acting as a container for the collectionView for a test. I simply forgot to remove it, all is working fine with a nice clearColor now...
Watch out when setting UICollectionViews Background Color in Storyboard:
The initially selected value Default is Black (counterintuitively).
You have to explicitly select Clear Color for the View to be transparent.
Also note that the Preview in Storyboard immediately changes when this is done 'right'...
The easiest solution is just pick anything color in color picker to change collectionview background then turn the opacity to 0%.
I solved it using in Swift 3:
collectionViewVideo.backgroundColor = UIColor.clear.withAlphaComponent(0)
Fogmeister's answer worked great. Adapted to Swift 3, it would be:
self.collectionView.backgroundColors = [NSColor.clear]
self.collectionView.backgroundView = NSView.init(frame: CGRect.zero)
Swift 4.0 from Fogmeister's answer
self.collectionView.backgroundColor = UIColor.clear
self.collectionView.backgroundView = UIView.init(frame: CGRect.zero)
To have a nice semi-transparent white background use:
collectionView.backgroundColor = UIColor(displayP3Red: 1.0, green: 1.0, blue: 1.0, alpha: 0.35)
In swift this work with me:
self.collectionView.backgroundColor = UIColor(red: 1, green: 1, blue: 1, alpha: 0.0)
What is did to fix it
From Storyboard set collection view background colour as clear colour
then set main view colour to any colour you want , (I set to white.)