简体   繁体   English

Swift 中的透明 UINavigationBar

[英]Transparent UINavigationBar in Swift

I am trying to make my UINavigationBar in UINavigationController transparent.我试图使UINavigationBar中的UINavigationController透明。 I created a subclass of UINavigationController and liked it to a scene in my storyboard file.我创建了UINavigationController一个子类,并喜欢它在我的故事板文件中的场景中。 Here's a piece of my subclass:这是我的子类的一部分:

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上做这样的事情 - 它有效。

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.2 to Swift 5.1 Swift 4.2Swift 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 appearance to any value. 在Info.plist中,您需要将基于View控制器的状态栏外观定义为任何值。

在此输入图像描述

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 - 大型标题(测试截图)

大标题 -  Swift  -  iPhone XR

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

Swift 3.0.1 with Xcode 8.1 带Xcode 8.1的Swift 3.0.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
}

Xcode 8.x : Swift 3: Extension for the same Write once use throughout Xcode 8.x:Swift 3:扩展为相同的写入一次使用

extension UINavigationBar {

    func transparentNavigationBar() {
        self.setBackgroundImage(UIImage(), for: .default)
        self.shadowImage = UIImage()
        self.isTranslucent = true
    }
} 

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
    }
}

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 fe), not UIScrollView content through navigation bar, then you need to set subview's frame in viewDidAppear . 如果要绘制常规子视图(Google map fe),而不是通过导航栏绘制UIScrollView内容,则需要在viewDidAppear设置子视图的帧。 So step 1: 所以第1步:

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. 这对我有用。

This will definitely work for swift 4/5 users.这肯定适用于 swift 4/5 用户。

func setUpNavBar(){
    navigationItem.title = "Flick"
    navigationController?.navigationBar.shadowImage = UIImage()
    self.navigationController?.navigationBar.setBackgroundImage(UIImage(), for: UIBarMetrics.default)
    self.navigationController?.navigationBar.shadowImage = UIImage()
    self.navigationController?.navigationBar.isTranslucent = true
    self.navigationController?.view.backgroundColor = UIColor.clear
    navigationController?.navigationBar.titleTextAttributes = [.foregroundColor: UIColor.white]
}

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

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