如何使UINavigationBar透明?虽然我希望它的栏项仍然可见。


当前回答

解决方案- Swift 5 - iOS 13+

根据文档,在你的UIViewController子类中:

override func viewDidLoad()
{
    super.viewDidLoad()
    
    let appearance = UINavigationBarAppearance()
    appearance.configureWithTransparentBackground()
    //appearance.backgroundColor = UIColor.clear
    
    navigationItem.compactAppearance = appearance
    navigationItem.scrollEdgeAppearance = appearance
    navigationItem.standardAppearance = appearance
    
    //...
}

只是为了澄清,这使得UINavigationBar完全透明。栏按钮项仍然可见,并正常工作。

什么不起作用

override func viewDidLoad()
{
    super.viewDidLoad()
    
    navigationController?.navigationBar.isTranslucent = true
    navigationController?.navigationBar.isOpaque = false

    //...
}

这让我意识到我其实不知道透明RIP和半透明RIP之间的区别。

参考文献

https://developer.apple.com/documentation/uikit/uinavigationcontroller/customizing_your_app_s_navigation_bar

https://www.lexico.com/en/definition/transparent

https://www.lexico.com/en/definition/translucent

更新08/10/2021

在以我提供的方式设置外观后,更改navigationItem栏按钮将重置外观,您必须再次执行此操作。

其他回答

解决方案- Swift 5 - iOS 13+

根据文档,在你的UIViewController子类中:

override func viewDidLoad()
{
    super.viewDidLoad()
    
    let appearance = UINavigationBarAppearance()
    appearance.configureWithTransparentBackground()
    //appearance.backgroundColor = UIColor.clear
    
    navigationItem.compactAppearance = appearance
    navigationItem.scrollEdgeAppearance = appearance
    navigationItem.standardAppearance = appearance
    
    //...
}

只是为了澄清,这使得UINavigationBar完全透明。栏按钮项仍然可见,并正常工作。

什么不起作用

override func viewDidLoad()
{
    super.viewDidLoad()
    
    navigationController?.navigationBar.isTranslucent = true
    navigationController?.navigationBar.isOpaque = false

    //...
}

这让我意识到我其实不知道透明RIP和半透明RIP之间的区别。

参考文献

https://developer.apple.com/documentation/uikit/uinavigationcontroller/customizing_your_app_s_navigation_bar

https://www.lexico.com/en/definition/transparent

https://www.lexico.com/en/definition/translucent

更新08/10/2021

在以我提供的方式设置外观后,更改navigationItem栏按钮将重置外观,您必须再次执行此操作。

IOS7:

self.navigationController.navigationBar.translucent = YES;
self.navigationController.navigationBar.shadowImage = [UIImage new];
self.navigationController.view.backgroundColor = [UIColor clearColor];
[self.navigationController.navigationBar setBackgroundImage:[UIImage new] forBarMetrics:UIBarMetricsDefault];
self.navigationController.navigationBar.backgroundColor = [UIColor clearColor];

试试下面这段代码:

self.navigationController.navigationBar.translucent = YES;

c# / Xamarin解决方案

NavigationController.NavigationBar.SetBackgroundImage(new UIImage(), UIBarMetrics.Default);
NavigationController.NavigationBar.ShadowImage = new UIImage();
NavigationController.NavigationBar.Translucent = true;

在按照上面大家说的去做之后,也就是:

navigationController?.navigationBar.setBackgroundImage(UIImage(), forBarMetrics: .default)
navigationController?.navigationBar.shadowImage = UIImage()
navigationController!.navigationBar.isTranslucent = true

... 我的导航栏还是白色的。所以我加了这一行:

navigationController?.navigationBar.backgroundColor = .clear

... 果不其然!这似乎奏效了。