How to make UINavigationBar background transparent? - ios

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

Related

How to set exact color to uinavigationbar as given, i need below

All i want that the UINavigationBar and the searchView(CustomView)
should have exact same color and without having shadow under the UINavigataionbar. i have tried a lot,i found the soultion for shadow
UINavigationBar.appearance().shadowImage = UIImage()
but still can't able to do same background color on UINavigationBar and searchView. You can see that minor color difference between UINavigationBar and SearchView
Please don't go for guidelines, if u have answer please share.
Note: SearcView is UIView wid label and an image not UISeachBar.
Try setting translucent property of navigationBar to false
Swift
self.navigationController.navigationBar.isTranslucent = false
Objective C
[self.navigationController.navigationBar setTranslucent:NO];
PS: if you want to do it in all the screen, set it globally
Swift
UINavigationBar.appearance().isTranslucent = false
Objective C
[[UINavigationBar appearance] setTranslucent:NO];
Set your UINavigationBar translucent property false.
Storyboard: In Storyboard uncheck translucent property
Swift:
self.navigationController.navigationBar.isTranslucent = false
In AppDelegate (didFinishLaunchingWithOptions):
UINavigationBar.appearance().isTranslucent = false
You can change the color of navigation bar using this
UINavigationBar *bar = [self.navigationController navigationBar];
[bar setTintColor:[UIColor greenColor]]; //Change the color according to your requirement.
Try This One
Objective C
self.navigationController.navigationBarHidden=NO;
[self.navigationController.navigationBar setTitleTextAttributes: #{NSForegroundColorAttributeName:White,NSFontAttributeName:yourFont}];
self.navigationController.navigationBar.barTintColor = yourColor;
Swift
self.navigationController?.navigationBar.barTintColor = .red //yourColor
self.navigationController?.navigationBar.tintColor = .white //yourColor

Top space to superview does not work

I want to set an UIImageView under the NavigationBar like Twitter profile.
I create a constraint on the top of the UIImageView to superview but it does not work, the UIImageView display below NavigationBar.
Why ?
Storyboard :
Screen :
Code :
self.navigationController!.navigationBar.setBackgroundImage(UIImage(), for: .default)
self.navigationController!.navigationBar.shadowImage = UIImage()
self.navigationController!.navigationBar.isTranslucent = true
Constraints :
Along with your code
Add the following line of code:
self.navigationController?.navigationBar.backgroundColor = .clear
Make sure to have your view controller extend under the navigation bar. You can do this in the storyboard like shown:
Setting the background color of your navigation bar doesn't seem to actually set it to clear, it's not an issue with the constraints.
I had the exact same problem, try the following code inside viewDidLoad instead.
Swift
self.navigationController?.navigationBar.setBackgroundImage(UIImage(), for: .default)
self.navigationController?.navigationBar.shadowImage = UIImage()
self.navigationController?.navigationBar.isTranslucent = true
Obj-C
[self.navigationController.navigationBar setBackgroundImage: [[UIImage alloc] init] forBarMetrics:UIBarMetricsDefault];
[self.navigationController.navigationBar setShadowImage:[[UIImage alloc] init]];
self.navigationController.navigationBar.translucent = true;
I think view controller property Under Top Bars is disable.
Enable this property as shown in above figure and your problem will solve.

How can I remove border bottom of UINavigationBar?

I want to remove the bottom border from UINavigationBar, but I don't know how to remove it.
Actually, that is the shadow of the navigation bar.
To get rid of it, just set it to an empty image:
navigationController.navigationBar.shadowImage = UIImage()
Note: You must set it to an empty UIImage(); nil won't work for some reason.
You need to set a custom shadow image to show instead of the default one. Note: a custom background image must also be set.
navController.navigationBar.barTintColor = .blue //set your color
navController.navigationBar.isTranslucent = false
navController.navigationBar.setBackgroundImage(UIImage(), for: .default)
navController.navigationBar.shadowImage = UIImage()
you can do this
self.navigationController.navigationBar.layer.borderWidth = 0.0;
OR
you can give border color same as navigation bar background color
self.navigationController.navigationBar.layer.borderColor = [UIColor colorWithRed:<#(CGFloat)#> green:<#(CGFloat)#> blue:<#(CGFloat)#> alpha:<#(CGFloat)#>];
For iOS 11 you can use the (deprecated) Black Translucent Navigation bar style with a custom bar tint.

Background color for UISearchController in UITableView

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)

Translucent navigation and tab bars appearing opaque despite enabling transparency

I'm trying to create a translucent tab and navigation bar using my custom colors however nothing I try enables the transparency. I've looked online and most of them say the translucent boolean is set to true by default however even when I set it as well the bars still stay opaque.
UINavigationBar.appearance().barTintColor = UIColor.blueColor()
UINavigationBar.appearance().tintColor = UIColor.yellowColor()
UINavigationBar.appearance().translucent = true
UITabBar.appearance().tintColor = UIColor.yellowColor()
UITabBar.appearance().barTintColor = UIColor.blueColor()
UITabBar.appearance().translucent = true
Yes I've also tried using alpha values.
Try setting the background colour on the window in applicationDidFinishLaunching
application.window.backgroundColor = [UIColor redColor];

Resources