Swift中的透明UINavigationBar [英] Transparent UINavigationBar in Swift

查看:83
本文介绍了Swift中的透明UINavigationBar的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试在 UINavigationController 透明中创建 UINavigationBar 。我创建了一个 UINavigationController 的子类,并喜欢它到我的storyboard文件中的一个场景。这是我的子类的一部分:

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:

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.

例如,如果我在 UITabBar 上做了这样的事情 - 它就可以了。

For example if I do such thing on UITabBar - it works.

推荐答案

希望能帮到你

Swift 2:

self.navigationController.navigationBar.setBackgroundImage(UIImage(), forBarMetrics: UIBarMetrics.Default)
self.navigationController.navigationBar.shadowImage = UIImage()
self.navigationController.navigationBar.translucent = true
self.navigationController.view.backgroundColor = UIColor.clearColor()

Swift 4

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 :

在Info.plist中,您需要定义查看基于控制器的状态栏外观到任何值。

In your Info.plist you need to define View controller-based status bar appearance to any value.

UIApplication.shared.statusBarStyle = .lightContent

如果你想隐藏状态栏:

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.

这篇关于Swift中的透明UINavigationBar的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆