简体   繁体   English

如何在swift4中清除导航栏颜色?

[英]How to remove navigation bar colour clear in swift4?

I want to clear color of navigation bar.我想清除导航栏的颜色。 In my ViewController there is a background image on that, when i remove color of navigation barTintColor, navigationController.view.background and navigation background image then simulator shows me :-在我的 ViewController 中有一个背景图像,当我删除导航 barTintColor、navigationController.view.background 和导航背景图像的颜色时,模拟器会显示我:- 在此处输入图像描述

I have been trying alots of codes but there is no solution found.我一直在尝试很多代码,但没有找到解决方案。 I want navigation Bar like that:-我想要这样的导航栏:- 在此处输入图像描述

with clear navigation bar color.带有清晰的导航栏颜色。 Is there any solution, let me know?有什么解决办法,告诉我? Thanks!谢谢!

You can make the navigation bar transparent in viewWillAppear and remove transparency in viewWillDisappear as follows 您可以在导航栏上透明的viewWillAppear ,并移除透明度viewWillDisappear如下

override func viewWillAppear(_ animated: Bool) {
    super.viewWillAppear(animated)
    self.navigationController?.navigationBar.setBackgroundImage(UIImage(), for: .default)
    self.navigationController?.navigationBar.shadowImage = UIImage()
    self.navigationController?.navigationBar.isTranslucent = true
}
override func viewWillDisappear(_ animated: Bool) {
    super.viewWillDisappear(animated)
    self.navigationController?.navigationBar.setBackgroundImage(nil, for: .default)
    self.navigationController?.navigationBar.shadowImage = nil
    self.navigationController?.navigationBar.isTranslucent = false
}

The background image and the back button will be visible 背景图像和后退按钮将可见

在此处输入图片说明

Better you must avoid the navigation bar. 最好您必须避开导航栏。 Hide the navigation bar in the navigation controller and user custom view in your view controller to avoid this issue. 将导航栏隐藏在导航控制器中,并将用户自定义视图隐藏在视图控制器中,以避免出现此问题。

Swift 5::<\/strong> Calling below in AppDelegate's didFinishLaunchingWithOptions function does the trick (This will be applied to your all navigationBars though, don't forget to switch your view controllers) Swift 5::<\/strong>在 AppDelegate 的 didFinishLaunchingWithOptions 函数中调用下面的方法可以解决问题(这将应用于您的所有导航栏,但不要忘记切换您的视图控制器)

let navBarAppearance = UINavigationBarAppearance()
navBarAppearance.configureWithTransparentBackground()
            
navigationController?.navigationBar.standardAppearance = navBarAppearance
navigationController?.navigationBar.scrollEdgeAppearance = navBarAppearance

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM