user3492165
user3492165

Reputation: 99

UINavigationController transparency?

I am trying to get my navigation bar to become 100% transparent, so that the UINavigationButtonItems are only visible and the background (normally white) should show the background image.

I tried

HomeNavigationController *navBar = [[HomeNavigationController alloc] initWithRootViewController:self.myViewController];
[navBar.navigationBar setBarTintColor:[UIColor clearColor]];
    [navBar.navigationBar setTranslucent:YES];

although neither appear to work.

EDIT:

Using

[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 can see that it worked as expected, however the items are now also invisible.


FINALEDIT: Ah the code above DOES work, just ensure you do not apply any other changes to the appearance without testing first !

The code that actually does what is intended:

[self.navigationController.navigationBar setBackgroundImage:[UIImage new]
                                                  forBarMetrics:UIBarMetricsDefault];

Alternatively you can set a transparent image, but this makes more sense. You will need the rest of the code shown in the original edit if you want no line representing the border.

Upvotes: 6

Views: 4598

Answers (1)

Krivoblotsky
Krivoblotsky

Reputation: 1502

There is a trick. Just set transparent image to the navigation bar background.

UIImage *fakeImage = [UIImage imageNamed:@"transparentImage"];
[navigationBar setBackgroundImage:fakeImage forBarMetrics:UIBarMetricsDefault];

OR

[navigationBar setBackgroundImage:[UIImage new]
                    forBarMetrics:UIBarMetricsDefault];

Upvotes: 6

Related Questions