透明的UINavigationBar

12

我是iOS的新手,找到了这个使UINavigationBar透明的解决方案。 在我的项目文件中,我应该把这段代码放在哪里?

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

这样它就能应用于我整个项目中使用了导航控制器的地方。


好的..你有什么问题? - iPatel
viewDidLoad 方法中或者在你的 appDelegate 文件的 didFinishLunchWithOptions 方法中。 - Dimitris Bouzikas
我把那段代码粘贴到了我的appDelegate中,但它并没有使导航栏透明。我需要改变什么吗?抱歉..我刚开始学习iOS :) - Nyaga Kennedy
也许你忘记了这个 self.navigationController.view.backgroundColor = [UIColor clearColor]; - Dimitris Bouzikas
1
当我引入[UINavigationBar appearance].translucent = YES;时,应用程序会崩溃。我得到的错误是:Terminating app due to uncaught exception 'NSInvalidArgumentException',reason: '*** Illegal property type, c for appearance setter, _installAppearanceSwizzlesForSetter:' - Nyaga Kennedy
这是我在appDelegate中的代码: [[UINavigationBar appearance] setBackgroundImage:[UIImage new]forBarMetrics:UIBarMetricsDefault]; [UINavigationBar appearance].shadowImage = [UIImage new]; [UINavigationBar appearance].translucent = YES; 但是当我运行时,应用程序崩溃并显示以下错误:Terminating app due to uncaught exception 'NSInvalidArgumentException',reason: '*** Illegal property type, c for appearance setter, _installAppearanceSwizzlesForSetter:' 通过使用断点,它指向设置半透明属性(translucent)的地方。 - Nyaga Kennedy
5个回答

31

在您的根视图控制器的viewDidLoad函数中添加以下代码:

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
    }

这一定有效!透明的UINavigationBar


这是我在appDelegate中的代码: [[UINavigationBar appearance] setBackgroundImage:[UIImage new]forBarMetrics:UIBarMetricsDefault]; [UINavigationBar appearance].shadowImage = [UIImage new]; [UINavigationBar appearance].translucent = YES; 但是当我运行时,应用程序崩溃并显示以下错误:Terminating app due to uncaught exception 'NSInvalidArgumentException',reason: '*** Illegal property type, c for appearance setter, _installAppearanceSwizzlesForSetter:' 通过使用断点,它指向设置半透明属性(translucent)的地方。 - Nyaga Kennedy
3
如果我可以再多点五个+1,我一定会这么做。这是一个很好的解决方案。 - Katushai
如何在推送下一个视图时取消导航栏的透明度 - Chlebta
只需使用相同的代码撤销此操作,但设置您喜欢的颜色并将“translucent”设置为“NO”。 - Dimitris Bouzikas
你刚刚为我节省了大量时间。非常感谢你提供的解决方案。 - Tariq
显示剩余3条评论

2

如果你想改变所有应用程序的外观,我建议你使用以下方法:

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

希望这能帮到您


我已经尝试过了,但它并没有变成透明的。 - Nyaga Kennedy

1
在您的UIViewController类中,您还可以使用UIAppearance机制http://nshipster.com/uiappearance/ 并放置这个。
    [[UINavigationBar appearance] setBackgroundImage:[UIImage new]
forBarMetrics:UIBarMetricsDefault];
    [UINavigationBar appearance].shadowImage = [UIImage imageNamed:@"Your image file here"];

转化为

- ( BOOL ) application:( UIApplication* ) application didFinishLaunchingWithOptions:( NSDictionary* ) launchOptions

当我加入这行代码[UINavigationBar appearance].translucent = YES;时,应用程序会崩溃。 - Nyaga Kennedy
我遇到了以下错误:终止应用程序,原因是未捕获的异常 'NSInvalidArgumentException',原因是:'***非法属性类型,c 用于外观设置器,_installAppearanceSwizzlesForSetter:'。 - Nyaga Kennedy
是的,抱歉。translucent 不是外观选择器。您可以在此处找到完整的外观选择器列表 https://gist.github.com/mattt/5135521。 - Avt

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

-4

设置以下代码

Self.navigationcontroller.navigationbar.transculant=yes;

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接