How to make completely transparent navigation bar

2019-01-07 01:37发布

I want the UINavigationBar in my app to be completely transparent and flush with the viewcontroller directly under it. However, the only code I could find makes it translucent but not transparent. I know this can be done in iOS 7 because it is used in the notes app. My question is, what is the code they used to do it?

8条回答
男人必须洒脱
2楼-- · 2019-01-07 02:17

Use UINavigationBar+Addition pod, then simply call:

UINavigationBar *navigationBar = self.navigationController.navigationBar;
[navigationBar makeTransparent];
查看更多
够拽才男人
3楼-- · 2019-01-07 02:18

Alan forgot one line

self.navigationController.navigationBar.backgroundColor = [UIColor clearColor];

So I have:

[self.navigationController.navigationBar setTranslucent:YES];
self.navigationController.view.backgroundColor = [UIColor clearColor];
[self.navigationController.navigationBar setBackgroundImage:[[UIImage alloc] init] forBarMetrics:UIBarMetricsDefault];
self.navigationController.navigationBar.shadowImage = [[UIImage alloc] init];
self.navigationController.navigationBar.backgroundColor = [UIColor clearColor];
查看更多
疯言疯语
4楼-- · 2019-01-07 02:21

[(UIView*)[self.navigationController.navigationBar.subviews objectAtIndex:0] setAlpha:0.0f];

That one line seemed to work perfectly for me

查看更多
Luminary・发光体
5楼-- · 2019-01-07 02:23

For Swift3 and Swift4

self.navigationController?.navigationBar.setBackgroundImage(UIImage(), for: .default)
self.navigationController?.navigationBar.shadowImage = UIImage()
self.navigationController?.navigationBar.isTranslucent = true

For Swift2.2

 self.navigationController?.navigationBar.setBackgroundImage(UIImage(), forBarMetrics: .Default)
 self.navigationController?.navigationBar.shadowImage = UIImage()
 self.navigationController?.navigationBar.translucent = true

For Objective-C

[self.navigationController.navigationBar setBackgroundImage:[UIImage new] forBarMetrics:UIBarMetricsDefault];
self.navigationController.navigationBar.shadowImage = [UIImage new];
self.navigationController.navigationBar.translucent = YES;
查看更多
Explosion°爆炸
6楼-- · 2019-01-07 02:28

From this answer

[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];
self.navigationController.navigationBar.backgroundColor = [UIColor clearColor];

Also, as suggested by Josh in the comments, to put the bar back to default:

[self.navigationController.navigationBar setBackgroundImage:nil
                     forBarMetrics:UIBarMetricsDefault];
查看更多
干净又极端
7楼-- · 2019-01-07 02:28

Swift 4.2 and iOS 12

It turns out all you really need is the code below. It works perfectly when you put it into viewDidLoad().

// removes line at bottom of navigation bar
navigationController?.navigationBar.shadowImage = UIImage()

// makes navigation bar completely transparent
navigationController?.navigationBar.setBackgroundImage(UIImage(), for: .default)
navigationController?.navigationBar.isTranslucent = true
查看更多
登录 后发表回答