【问题标题】:How to fix transparency of navigation bar in swift?如何快速修复导航栏的透明度?
【发布时间】:2016-09-09 20:33:36
【问题描述】:

我有一个带有视图控制器背景图像的透明导航栏,但是当我向导航栏添加一个栏按钮项时,它就像第二张图片一样。我如何让栏按钮项也完全透明的导航栏。

我使用下面的这些代码使导航栏透明;

  extension UINavigationController {

  public func presentTransparentNavigationBar() {
    navigationBar.setBackgroundImage(UIImage(), forBarMetrics:UIBarMetrics.Default)
    navigationBar.translucent = true
    navigationBar.shadowImage = UIImage()
    setNavigationBarHidden(false, animated:true)
  }

  public func hideTransparentNavigationBar() {
    setNavigationBarHidden(true, animated:false)
    navigationBar.setBackgroundImage(UINavigationBar.appearance().backgroundImageForBarMetrics(UIBarMetrics.Default), forBarMetrics:UIBarMetrics.Default)
    navigationBar.translucent = UINavigationBar.appearance().translucent
    navigationBar.shadowImage = UINavigationBar.appearance().shadowImage
  }
}

【问题讨论】:

  • 您是否尝试为导航栏设置tintColor = UIColor.clearColor()
  • 不要发布指向您的代码的链接。使用您的实际相关代码更新您的问题。
  • 是的,但不明白为什么不工作

标签: ios swift


【解决方案1】:

这应该创建一个透明的UINavigationBar,其中包含项目。它目前对我来说工作正常。

    let navigationBarAppearace = UINavigationBar.appearance()
    navigationBarAppearace.tintColor           = UIColor.whiteColor()
    navigationBarAppearace.translucent         = true
    navigationBarAppearace.shadowImage         = UIImage()
    navigationBarAppearace.backgroundColor     = UIColor(red: 0.0, green: 0.0, blue: 0.0, alpha: 0.0)
    navigationBarAppearace.titleTextAttributes = [NSForegroundColorAttributeName : UIColor.whiteColor()]
    navigationBarAppearace.setBackgroundImage(UIImage(), forBarMetrics: .Default)

【讨论】:

    【解决方案2】:

    试试:

    if let navBar = self.navigationController?.navigationBar {
        extendedLayoutIncludesOpaqueBars = true    
        navigationBar.translucent = true
        navigationBar.backgroundColor = UIColor.clearColor()
        navigationBar.setBackgroundImage(UIImage(), forBarMetrics: .Default)
        navigationBar.shadowImage = UIImage()
    
    }
    

    【讨论】:

      猜你喜欢
      • 2019-12-29
      • 2018-09-21
      • 1970-01-01
      • 2018-11-27
      • 2021-11-03
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多