I am currently working on an iOS Application in Swift 3 and wanted to change the color of my NavigationBar
with the following code:
self.navigationController?.navigationBar.barTintColor = UIColor.init(red: 53.0/255.0, green: 70.0/255.0, blue: 90.0/255.0, alpha: 1.0)
This code works quite fine but there's one problem. The color I entered in RGB format is displayed wrong.
Should be like this color:
But looks like this (left: is current Color () right: as already said should look like):
Set navigationController?.navigationBar.isTranslucent = false.
You can also achieve this by unchecking Translucent from storyboard.
Change navigation bar to Opaque instead of Translucent.
Swift
self.navigationController?.navigationBar.isTranslucent = true
Objective-C
[[UINavigationBar appearance] setTranslucent:YES];
Please find in image.
And if you are setting navigations background color then change
navigation background color instead of tint color.
Related
This is very irritating.
In my AppDelegate, I set the navigation bar color like this and it works fine:
nav.navigationBar.backgroundColor = #colorLiteral(red: 0.1176, green: 0.251, blue: 0.4863, alpha: 1) /* #1e407c - B = 48% */
This line does not work anywhere else. I have another navigation controller and I want to set its bar color to the same color.
I tried putting this in viewDidLoad, viewWillAppear, and where I instantiate a new navigation controller.
I also tried setting the tint color.
Edit: It works when I set the barTintColor.
I'm trying to set a faded darker background to a navigation bar, as you can see on the image below.
I've tried with this code but it's not exactly what I expect.
UINavigationBar.appearance().backgroundColor = UIColor(red: 0, green: 0, blue: 0, alpha: 0.4)
I've found this topic, but without suitable answer:
Shows a faded gray color view through iOS 7 Navigation bar of a UISplitViewController
Any idea will be appreciated.
EDIT 6 oct 2015 : If somebody needs to know how to perform that
For Swift users :
- in particular viewcontroller : i use :
self.navigationController!.navigationBar.setBackgroundImage(UIImage(named: "fadedimage.png"), forBarMetrics: .Default)
in the main NavigationController (to apply that to the all navigationBar) i use :
UINavigationBar.appearance().setBackgroundImage(UIImage(named: "fadedimage.png"), forBarMetrics: .Default)
This might not be the correct way, but you can achieve the same by using a custom image.
Donwload a transparent image from internet, I named it as transparent.png
and set the navigation bar background image:
[self.navigationController.navigationBar setBackgroundImage:[UIImage imageNamed:#"transaprent.png" ] forBarMetrics:UIBarMetricsDefault];
[self.navigationController.navigationBar setBackgroundColor:[UIColor colorWithRed:0.0f green:0.0f blue:0.0f alpha:0.0f]];
This gives the result:
Later you can apply the shadow effect for the navigation bar.
Edit:
I donwloaded a custom shadow transparent png image, and applied it using above code.
You can still improve this with a better custom image as per your requirements.
Here is the image I used:
To remove the bottom border that appears on navbar, use this code:
[self.navigationController.navigationBar setShadowImage:[[UIImage alloc] init]];
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)
I want to change my tab bar items to be pink when selected instead of the default blue.
How can i accomplish this using the storyboard editor in Xcode 6?
Here are my current setting which are not working, the blue background works but the pink doesnt work:
Add Runtime Color attribute named "tintColor" from StoryBoard. This is working(for Xcode 8 and above).
if you want unselected color.. you can add unselectedItemTintColor too.
This elegant solution works great on SWIFT 3.0, SWIFT 4.2 and SWIFT 5.1:
On the Storyboard:
Select your Tab Bar
Set a Runtime Attibute called tintColor for the desired color of the Selected Icon on the tab bar
Set a Runtime Attibute called unselectedItemTintColor for the desired color of the Unselected Icon on the tab bar
Edit: Working with Xcode 8/10, for iOS 10/12 and above.
In Swift, using xcode 7 (and later), you can add the following to your AppDelegate.swift file:
UITabBar.appearance().tintColor = UIColor(red: 255/255.0, green: 255/255.0, blue: 255/255.0, alpha: 1.0)
This is the what the complete method looks like:
func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool {
// I added this line
UITabBar.appearance().tintColor = UIColor(red: 255/255.0, green: 255/255.0, blue: 255/255.0, alpha: 1.0)
return true
}
In the example above my item will be white. The "/255.0" is needed because it expects a value from 0 to 1. For white, I could have just used 1. But for other color you'll probably be using RGB values.
On Xcode8 I have changed the ImageTint from the storyboard and it works well.
The result:
Swift 3 | Xcode 10
If you want to make all tab bar items the same color (selected & unselected)...
Step 1
Make sure your image assets are setup to Render As = Template Image. This allows them to inherit color.
Step 2
Use the storyboard editor to change your tab bar settings as follows:
Set Tab Bar: Image Tint to the color you want the selected icon to inherit.
Set Tab Bar: Bar Tint to the color you want the tab bar to be.
Set View: Tint to the color you want to see in the storyboard editor, this doesn't affect the icon color when your app is run.
Step 3
Steps 1 & 2 will change the color for the selected icon. If you still want to change the color of the unselected items, you need to do it in code. I haven't found a way to do it via the storyboard editor.
Create a custom tab bar controller class...
// TabBarController.swift
class TabBarController: UITabBarController {
override func viewDidLoad() {
super.viewDidLoad()
// make unselected icons white
self.tabBar.unselectedItemTintColor = UIColor.white
}
}
... and assign the custom class to your tab bar scene controller.
If you figure out how to change the unselected icon color via the storyboard editor please let me know. Thanks!
This best way is to change Image Tint in storyboard
put this code in the viewDidLoad of the view controller that you want to change the color of
[[UITabBar appearance] setSelectedImageTintColor:[UIColor whiteColor]];
You can also set selected image bar tint color by key path:
Hope this will help you!! Thanks
XCode 8.2, iOS 10, Swift 3: now there's an unselectedItemTintColor attribute for tabBar:
self.tabBar.unselectedItemTintColor = UIColor(red: 0/255.0, green: 200/255.0, blue: 0/255.0, alpha: 1.0)
You can change colors UITabBarItem by storyboard but if you want to change colors by code it's very easy:
// Use this for change color of selected bar
[[UITabBar appearance] setTintColor:[UIColor blueColor]];
// This for change unselected bar (iOS 10)
[[UITabBar appearance] setUnselectedItemTintColor:[UIColor yellowColor]];
// And this line for change color of all tabbar
[[UITabBar appearance] setBarTintColor:[UIColor whiteColor]];
Somehow we are not able to change the Tab Bar selected item Tint color using storyboard alone, hence I added below code in my ViewDidLoad, hope this helps.
[[UITabBar appearance] setTintColor:[UIColor whiteColor]];
Add this code in your app delegate -did_finish_launching_with_options function
UITabBar.appearance().tintColor = UIColor( red: CGFloat(255/255.0), green: CGFloat(99/255.0), blue: CGFloat(95/255.0), alpha: CGFloat(1.0) )
put the RGB of the required color
Image Tint from storyboard worked for me.
This is the solution in Swift 3 that works in iOS 10:
Firstly, you create your own tab bar controller subclass and add it to your tab controller in your storyboard. In the viewDidLoad() method you can then customize the tab bar. It should be stated here that the tintColor attribute of the tabBar represents the color of the selected item not the color of the unselected ones! In order to change the color of the unselected items, I recommend looping through each item and use the original colors of your images, so they are not rendered as grey automatically.
class CustomTabBarVC: UITabBarController
{
override func viewDidLoad()
{
super.viewDidLoad()
self.tabBar.tintColor = AppColor.normalRed
self.tabBar.barTintColor = .white
self.tabBar.isTranslucent = true
if let items = self.tabBar.items
{
for item in items
{
if let image = item.image
{
item.image = image.withRenderingMode( .alwaysOriginal )
}
}
}
}
}
The only downside with this approach is that your item images must already have the desired color you aim for.
You can subclass the UITabBarController, and replace the one with it in the storyboard.
In your viewDidLoad implementation of subclass call this:
[self.tabBar setTintColor:[UIColor greenColor]];
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.