Swift中的透明UINavigationBar

Nik*_*nov 27 uinavigationbar ios swift

我想让我UINavigationBarUINavigationController透明.我创建了一个子类,UINavigationController并喜欢它在我的storyboard文件中的一个场景.这是我的子类的一部分:

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
}
Run Code Online (Sandbox Code Playgroud)

当我运行我的应用程序时,导航栏是透明的,但状态栏只是黑色.

例如,如果我做这样的事情UITabBar- 它的工作原理.

Ash*_*kad 98

希望它对你有所帮助

斯威夫特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()
Run Code Online (Sandbox Code Playgroud)

Swift 4.2

self.navigationController?.navigationBar.setBackgroundImage(UIImage(), for: UIBarMetrics.default)
self.navigationController?.navigationBar.shadowImage = UIImage()
self.navigationController?.navigationBar.isTranslucent = true
self.navigationController?.view.backgroundColor = UIColor.clear
Run Code Online (Sandbox Code Playgroud)

或者如果您想要升级导航控制器,请参考此答案.


通过以下方式更改状态栏样式:

在Info.plist中,您需要将基于View控制器的状态栏外观定义为任何值.

在此输入图像描述

UIApplication.shared.statusBarStyle = .lightContent
Run Code Online (Sandbox Code Playgroud)

如果要隐藏状态栏:

UIApplication.shared.isStatusBarHidden = true
Run Code Online (Sandbox Code Playgroud)

通过轻量内容和透明导航获得此输出.我有查看背景是灰色的.你可以看到透明度.

在此输入图像描述

iPhone XR - Swift 4.2 - 大型标题(测试截图)

大标题 -  Swift  -  iPhone XR


Cha*_*van 29

如果您使用的是Swift 2.0,请使用下面的代码块:

 self.navigationController?.navigationBar.setBackgroundImage(UIImage(), forBarMetrics: UIBarMetrics.Default)
 self.navigationController?.navigationBar.shadowImage = UIImage()
 self.navigationController?.navigationBar.translucent = true
Run Code Online (Sandbox Code Playgroud)

对于Swift 3.0使用:

navigationController?.setNavigationBarHidden(false, animated: true)
navigationController?.navigationBar.setBackgroundImage(UIImage(), for: UIBarMetrics.default)
navigationController?.navigationBar.shadowImage = UIImage()
navigationController?.navigationBar.isTranslucent = true
Run Code Online (Sandbox Code Playgroud)


Jef*_*ang 5

带Xcode 8.1的Swift 3.0.1

在你的 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
}
Run Code Online (Sandbox Code Playgroud)


Ank*_*pta 5

Xcode 8.x:Swift 3:扩展同一写入一次使用

extension UINavigationBar {

    func transparentNavigationBar() {
        self.setBackgroundImage(UIImage(), for: .default)
        self.shadowImage = UIImage()
        self.isTranslucent = true
    }
} 
Run Code Online (Sandbox Code Playgroud)


use*_*088 5

创建扩展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
    }
}
Run Code Online (Sandbox Code Playgroud)