首页
学习
活动
专区
工具
TVP
发布
社区首页 >问答首页 >Swift中的透明UINavigationBar

Swift中的透明UINavigationBar
EN

Stack Overflow用户
提问于 2015-05-30 19:34:47
回答 5查看 54.3K关注 0票数 33

我正在尝试使我在UINavigationController中的UINavigationBar是透明的。我创建了UINavigationController的一个子类,并将其添加到我的故事板文件中的场景中。下面是我的子类的一部分:

代码语言:javascript
复制
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
}

当我运行我的应用程序时,有一个导航栏是透明的,但是状态栏是黑色的。

例如,如果我在UITabBar上做这样的事情-它是有效的。

EN

回答 5

Stack Overflow用户

发布于 2016-03-10 18:47:04

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

代码语言:javascript
复制
 self.navigationController?.navigationBar.setBackgroundImage(UIImage(), forBarMetrics: UIBarMetrics.Default)
 self.navigationController?.navigationBar.shadowImage = UIImage()
 self.navigationController?.navigationBar.translucent = true

对于Swift 3.0使用:

代码语言:javascript
复制
navigationController?.setNavigationBarHidden(false, animated: true)
navigationController?.navigationBar.setBackgroundImage(UIImage(), for: UIBarMetrics.default)
navigationController?.navigationBar.shadowImage = UIImage()
navigationController?.navigationBar.isTranslucent = true
票数 31
EN

Stack Overflow用户

发布于 2016-11-08 20:12:49

带有Xcode 8.1的Swift 3.0.1

在你的UINavigationController

代码语言:javascript
复制
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
}
票数 6
EN

Stack Overflow用户

发布于 2016-12-15 15:43:38

Xcode 8.x : Swift 3:同一个一次写入的扩展

代码语言:javascript
复制
extension UINavigationBar {

    func transparentNavigationBar() {
        self.setBackgroundImage(UIImage(), for: .default)
        self.shadowImage = UIImage()
        self.isTranslucent = true
    }
} 
票数 5
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/30545663

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档