ios Swift 中的透明 UINavigationBar
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/30545663/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Transparent UINavigationBar in Swift
提问by Nikita Zernov
I am trying to make my UINavigationBar
in UINavigationController
transparent. I created a subclass of UINavigationController
and liked it to a scene in my storyboard file. Here's a piece of my subclass:
我正在努力使我UINavigationBar
的信息UINavigationController
透明。我创建了一个子类,UINavigationController
并喜欢它在我的故事板文件中的一个场景中。这是我的子类的一部分:
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view.
let size = self.navigationBar.frame.size
self.navigationBar.setBackgroundImage(imageWithColor(UIColor.blackColor(), size: size, alpha: 0.2), forBarMetrics: UIBarMetrics.Default)
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
func imageWithColor(color: UIColor, size: CGSize, alpha: CGFloat) -> UIImage {
UIGraphicsBeginImageContext(size)
let currentContext = UIGraphicsGetCurrentContext()
let fillRect = CGRectMake(0, 0, size.width, size.height)
CGContextSetFillColorWithColor(currentContext, color.CGColor)
CGContextSetAlpha(currentContext, alpha)
CGContextFillRect(currentContext, fillRect)
let retval: UIImage = UIGraphicsGetImageFromCurrentImageContext()
UIGraphicsEndImageContext()
return retval
}
When I run my application a have a navigation bar transparent, but status bar is just black.
当我运行我的应用程序时,导航栏是透明的,但状态栏是黑色的。
For example if I do such thing on UITabBar
- it works.
例如,如果我做这样的事情UITabBar
- 它有效。
回答by Ashish Kakkad
Hope it help you
希望对你有帮助
Swift 2:
斯威夫特 2:
self.navigationController.navigationBar.setBackgroundImage(UIImage(), forBarMetrics: UIBarMetrics.Default)
self.navigationController.navigationBar.shadowImage = UIImage()
self.navigationController.navigationBar.isTranslucent = true
self.navigationController.view.backgroundColor = UIColor.clearColor()
Swift 4.2to Swift 5.1
Swift 4.2到Swift 5.1
self.navigationController?.navigationBar.setBackgroundImage(UIImage(), for: UIBarMetrics.default)
self.navigationController?.navigationBar.shadowImage = UIImage()
self.navigationController?.navigationBar.isTranslucent = true
self.navigationController?.view.backgroundColor = UIColor.clear
Or If you want to sublcass the navigation controller then refer this answer.
或者,如果您想转用导航控制器,请参考此答案。
Change the status bar style via :
通过以下方式更改状态栏样式:
In your Info.plist you need to define View controller-based status bar appearanceto any value.
在您的 Info.plist 中,您需要将基于视图控制器的状态栏外观定义为任何值。
UIApplication.shared.statusBarStyle = .lightContent
If you want to hide the status bar:
如果要隐藏状态栏:
UIApplication.shared.isStatusBarHidden = true
Getting this output by light content and by transparent navigation. I have view background is gray. you can see the transparency.
通过轻量内容和透明导航获取此输出。我有视图背景是灰色的。你可以看到透明度。
iPhone XR - Swift 4.2 - Large Titles (Test Screenshot)
iPhone XR - Swift 4.2 - 大标题(测试截图)
回答by Chamath Jeevan
If you're using Swift 2.0 uses the code block below:
如果您使用的是 Swift 2.0,请使用以下代码块:
self.navigationController?.navigationBar.setBackgroundImage(UIImage(), forBarMetrics: UIBarMetrics.Default)
self.navigationController?.navigationBar.shadowImage = UIImage()
self.navigationController?.navigationBar.translucent = true
For Swift 3.0 use:
对于 Swift 3.0 使用:
navigationController?.setNavigationBarHidden(false, animated: true)
navigationController?.navigationBar.setBackgroundImage(UIImage(), for: UIBarMetrics.default)
navigationController?.navigationBar.shadowImage = UIImage()
navigationController?.navigationBar.isTranslucent = true
回答by user550088
Create an extension of UINavigationController
and present or hide transparent navigation bar.
创建扩展UINavigationController
并显示或隐藏透明导航栏。
extension UINavigationController {
public func presentTransparentNavigationBar() {
navigationBar.setBackgroundImage(UIImage(), for:UIBarMetrics.default)
navigationBar.isTranslucent = true
navigationBar.shadowImage = UIImage()
setNavigationBarHidden(false, animated:true)
}
public func hideTransparentNavigationBar() {
setNavigationBarHidden(true, animated:false)
navigationBar.setBackgroundImage(UINavigationBar.appearance().backgroundImage(for: UIBarMetrics.default), for:UIBarMetrics.default)
navigationBar.isTranslucent = UINavigationBar.appearance().isTranslucent
navigationBar.shadowImage = UINavigationBar.appearance().shadowImage
}
}
回答by Jeff Gu Kang
Swift 3.0.1 with Xcode 8.1
Swift 3.0.1 与 Xcode 8.1
In your UINavigationController
在你的 UINavigationController
override func viewDidLoad() {
super.viewDidLoad()
self.navigationBar.setBackgroundImage(UIImage(), for: UIBarMetrics.default)
self.navigationBar.shadowImage = UIImage()
self.navigationBar.isTranslucent = true
self.view.backgroundColor = UIColor.clear
}
回答by Ankit Kumar Gupta
Xcode 8.x : Swift 3: Extension for the same Write once use throughout
Xcode 8.x : Swift 3: 对同一个 Write 的扩展贯穿始终
extension UINavigationBar {
func transparentNavigationBar() {
self.setBackgroundImage(UIImage(), for: .default)
self.shadowImage = UIImage()
self.isTranslucent = true
}
}
回答by Alexander Stepanishin
I tried all methods above and still got white space instead of content that supposed to be rendered through. If you want to draw regular subview (Google map f.e.), not UIScrollView content through navigation bar, then you need to set subview's frame in viewDidAppear
.
So step 1:
我尝试了上面的所有方法,但仍然得到空白而不是应该渲染的内容。如果你想通过导航栏绘制常规子视图(谷歌地图fe),而不是 UIScrollView 内容,那么你需要在viewDidAppear
. 所以第一步:
existingNavigationBar.setBackgroundImage(transparentImageFromAssets, for: .default)
existingNavigationBar.shadowImage = transparentImageFromAssets
existingNavigationBar.isTranslucent = true
step 2:
第2步:
override func viewDidAppear(_ animated: Bool) {
super.viewDidAppear(animated)
self.mapView.frame = UIScreen.main.bounds
}
This worked for me.
这对我有用。