透明的UINavigationBar

时间:2014-02-18 10:18:38

标签: ios uinavigationcontroller

是ios的新手,我找到了解决UINavigationBar透明的问题。 在我的项目文件中可以放置此代码

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

因此它应用于我正在使用导航控制器的整个项目中。

5 个答案:

答案 0 :(得分:31)

将rootViewController的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
    }

这肯定有用! Transparent UINavigationBar

答案 1 :(得分:2)

如果您想更改所有应用程序的外观,我建议您使用它:

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

希望有所帮助

答案 2 :(得分: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

答案 3 :(得分: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 :(得分:-4)

设置以下代码

Self.navigationcontroller.navigationbar.transculant=yes;