12

Am newbie to ios and I found this solution on making the UINavigationBar Transparent. Where in my project files can I put this code

[self.navigationBar setBackgroundImage:[UIImage new]
                     forBarMetrics:UIBarMetricsDefault];
self.navigationBar.shadowImage = [UIImage new];
self.navigationBar.translucent = YES;

So that it is applied in my entire project where navigation controller is being used.

Nyaga Kennedy
  • 453
  • 4
  • 11
  • Ok.. What is your problem ? – iPatel Feb 18 '14 at 10:22
  • In `viewDidLoad` or in your appDelegate file in `didFinishLunchWithOptions` – Dimitris Bouzikas Feb 18 '14 at 10:23
  • I pasted that code in my appDelegate but it is not making the navbar transparent.Is there something I should change? Sorry..Started learning ios :) – Nyaga Kennedy Feb 18 '14 at 10:27
  • Maybe you have forget this `self.navigationController.view.backgroundColor = [UIColor clearColor];` – Dimitris Bouzikas Feb 18 '14 at 10:28
  • 1
    The app crashes when i introduce [UINavigationBar appearance].translucent = YES; Error am getting is: Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '*** Illegal property type, c for appearance setter, _installAppearanceSwizzlesForSetter:' – Nyaga Kennedy Feb 18 '14 at 10:54
  • This is what i have in my appDelegate [[UINavigationBar appearance] setBackgroundImage:[UIImage new]forBarMetrics:UIBarMetricsDefault]; [UINavigationBar appearance].shadowImage = [UIImage new]; [UINavigationBar appearance].translucent = YES; When I run, the app crashes with this error Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '*** Illegal property type, c for appearance setter, _installAppearanceSwizzlesForSetter:' By use of breakpoint, It is pointing the error on setting translucent – Nyaga Kennedy Feb 18 '14 at 11:06

5 Answers5

31

Put in your viewDidLoad function of your rootViewController this code:

Objective-C:

[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];

Swift 2.x:

if let navigationBar = navigationController?.navigationBar {
        navigationBar.setBackgroundImage(UIImage(), forBarMetrics: .Default)
        navigationBar.shadowImage = UIImage()
        navigationBar.translucent = true
        navigationController?.view.backgroundColor = .clearColor()
    }

Swift 3:

if let navigationBar = navigationController?.navigationBar {
        navigationBar.setBackgroundImage(UIImage(), for: .default)
        navigationBar.shadowImage = UIImage()
        navigationBar.isTranslucent = true
        navigationController?.view?.backgroundColor = .clear
    }

This works for sure! Transparent UINavigationBar.

Community
  • 1
  • 1
Dimitris Bouzikas
  • 4,461
  • 3
  • 25
  • 33
  • This is what i have in my appDelegate [[UINavigationBar appearance] setBackgroundImage:[UIImage new]forBarMetrics:UIBarMetricsDefault]; [UINavigationBar appearance].shadowImage = [UIImage new]; [UINavigationBar appearance].translucent = YES; When I run, the app crashes with this error Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '*** Illegal property type, c for appearance setter, _installAppearanceSwizzlesForSetter:' By use of breakpoint, It is pointing the error on setting translucent – Nyaga Kennedy Feb 18 '14 at 11:08
  • 3
    if i could give 5 more +1's then i would. this is a great solution – Katushai May 22 '14 at 16:14
  • How to deactivate navbar transparency in next view when pushing – Chlebta Aug 26 '15 at 10:59
  • Just revert this using the same code but put the colour you prefer and set `translucent` to `NO`. – Dimitris Bouzikas Aug 26 '15 at 11:14
  • You just saved my tons of time. Thank you so much for the solution. – Tariq Sep 03 '15 at 04:35
  • This doesn't work for me. All I get a gray status bar and a completely white navigation bar with nothing visible. – Can Poyrazoğlu Jan 10 '16 at 15:20
  • @CanPoyrazoğlu did you check the example app? – Dimitris Bouzikas Jan 11 '16 at 07:16
  • @dimimpou yes. see my question http://stackoverflow.com/questions/34707190/how-to-make-uinavigationbar-background-transparent – Can Poyrazoğlu Jan 11 '16 at 07:22
2

If you want change the appearance for all your application, I recommend you to use this:

[[UINavigationBar appearance] setBackgroundImage:[UIImage new] forBarMetrics:UIBarMetricsDefault];

hope that will help

Nicolas Bonnet
  • 1,275
  • 11
  • 15
1

In your UIViewController class. You can also use UIAppearance mechanism http://nshipster.com/uiappearance/

And place this

    [[UINavigationBar appearance] setBackgroundImage:[UIImage new]
forBarMetrics:UIBarMetricsDefault];
    [UINavigationBar appearance].shadowImage = [UIImage imageNamed:@"Your image file here"];

into

- ( BOOL ) application:( UIApplication* ) application didFinishLaunchingWithOptions:( NSDictionary* ) launchOptions
Avt
  • 16,927
  • 4
  • 52
  • 72
  • The app crashes when i introduce this line [UINavigationBar appearance].translucent = YES; – Nyaga Kennedy Feb 18 '14 at 10:50
  • Am getting this error Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '*** Illegal property type, c for appearance setter, _installAppearanceSwizzlesForSetter:' – Nyaga Kennedy Feb 18 '14 at 10:53
  • Yes, sorry. translucent is not appearance selector. You can find full list of appearance selectors here https://gist.github.com/mattt/5135521 – Avt Feb 18 '14 at 11:04
0
Transparent UIToolbar:
self.toolbar.setBackgroundImage(UIImage(),
                                forToolbarPosition: UIBarPosition.Any,
                                barMetrics: UIBarMetrics.Default)
self.toolbar.setShadowImage(UIImage(),
                            forToolbarPosition: UIBarPosition.Any)
Transparent UINavigationBar:
self.navigationBar.setBackgroundImage(UIImage(), forBarMetrics: UIBarMetrics.Default)
self.navigationBar.shadowImage = UIImage()
self.navigationBar.translucent = true
Alvin George
  • 14,148
  • 92
  • 64
-4

set following code

Self.navigationcontroller.navigationbar.transculant=yes;
Divya Bhaloidiya
  • 5,018
  • 2
  • 25
  • 45
jayraj m.g.
  • 625
  • 5
  • 18