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]];
Related
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.
I am creating an iOS 7 app in which I'd like to have a SearchBar right bellow the NavigationBar, and I wanted them both to look like a single piece. Therefore I need to tint them with the same color (done already) and remove that hairline at the bottom of the NavigationBar and at the top of the SearchBar. How can I achieve that?
Officially, this is only possible by setting the shadowImage of the navigationBar to an empty image. However, a closer look at the documentation, it is said:
For a custom shadow image to be shown, a custom background image must also be set with the setBackgroundImage:forBarMetrics: method. If the default background image is used, then the default shadow image will be used regardless of the value of this property.
By using a custom background image, you would lose the blurred background translucency.
If you feel adventurous, the "hairline" is a UIImageView that is a subview of the navigation bar. You can find it and set it as hidden. This is what Apple does in their native calendar app, for example. Remember to show it when the current view disappears.
use following code in AppDelegate (didFinishLaunchingWithOptions)
Swift :
UINavigationBar.appearance().setBackgroundImage(UIImage(), for: .any, barMetrics: .default)
UINavigationBar.appearance().shadowImage = UIImage()
Objective c :
[[UINavigationBar appearance] setBackgroundImage:[[UIImage alloc] init]
forBarPosition:UIBarPositionAny
barMetrics:UIBarMetricsDefault];
[[UINavigationBar appearance] setShadowImage:[[UIImage alloc] init]];
Swift 3:
self.navigationController?.navigationBar.setValue(true, forKey: "hidesShadow")
One way to remove the hairline on the top and bottom of a UISearchBar is to replace the background image with a stretchable one, that does not have that thin border. Simply make a square shaped png with the color of your choice, then:
[searchBar setBackgroundImage:[[UIImage imageNamed:#"SearchBarImage"] resizableImageWithCapInsets:UIEdgeInsetsMake( 10, 10, 10, 10)]];
Since the background is solid you can use pretty much whatever values you want for the insets.
For those who interested how to implement "adventurous" approach of #Leo Natan, I added the sample code in my answer to the similar question.
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 am creating an iOS 7 app in which I'd like to have a SearchBar right bellow the NavigationBar, and I wanted them both to look like a single piece. Therefore I need to tint them with the same color (done already) and remove that hairline at the bottom of the NavigationBar and at the top of the SearchBar. How can I achieve that?
Officially, this is only possible by setting the shadowImage of the navigationBar to an empty image. However, a closer look at the documentation, it is said:
For a custom shadow image to be shown, a custom background image must also be set with the setBackgroundImage:forBarMetrics: method. If the default background image is used, then the default shadow image will be used regardless of the value of this property.
By using a custom background image, you would lose the blurred background translucency.
If you feel adventurous, the "hairline" is a UIImageView that is a subview of the navigation bar. You can find it and set it as hidden. This is what Apple does in their native calendar app, for example. Remember to show it when the current view disappears.
use following code in AppDelegate (didFinishLaunchingWithOptions)
Swift :
UINavigationBar.appearance().setBackgroundImage(UIImage(), for: .any, barMetrics: .default)
UINavigationBar.appearance().shadowImage = UIImage()
Objective c :
[[UINavigationBar appearance] setBackgroundImage:[[UIImage alloc] init]
forBarPosition:UIBarPositionAny
barMetrics:UIBarMetricsDefault];
[[UINavigationBar appearance] setShadowImage:[[UIImage alloc] init]];
Swift 3:
self.navigationController?.navigationBar.setValue(true, forKey: "hidesShadow")
One way to remove the hairline on the top and bottom of a UISearchBar is to replace the background image with a stretchable one, that does not have that thin border. Simply make a square shaped png with the color of your choice, then:
[searchBar setBackgroundImage:[[UIImage imageNamed:#"SearchBarImage"] resizableImageWithCapInsets:UIEdgeInsetsMake( 10, 10, 10, 10)]];
Since the background is solid you can use pretty much whatever values you want for the insets.
For those who interested how to implement "adventurous" approach of #Leo Natan, I added the sample code in my answer to the similar question.
Currently, I am able to change the tint of the my UINavigationBar by
self.navigationController.navigationBar.tintColor = [UIColor colorWithRed:74/255.0f green:74/255.0f blue:74/255.0f alpha:1.0f];
However when I try to implement the following code to change the image of the UINavigationBar all together, I see no results..
UIImage *navImageBackground = [[UIImage imageNamed:#"texturedNav"] //This being my .png image
resizableImageWithCapInsets:UIEdgeInsetsMake(0, 0, 0, 0)];
[[UINavigationBar appearance] setBackgroundImage:navImageBackground
forBarMetrics:UIBarMetricsDefault];
What am I doing wrong here?
You're probably calling the appearance delegate after the navigation bar is already on screen so your request is being 'ignored'. Or, at least, not causing a UI update.
Set the appearance before the navigation bar is shown. Or, force a UI refresh by hiding and showing the navigation bar (without animation).