【问题标题】:Change navigationbar color & title color on different screens更改不同屏幕上的导航栏颜色和标题颜色
【发布时间】:2018-06-27 10:22:13
【问题描述】:

在我的 iPhone 应用程序中,我有两个 NavigationC0nroller 背景颜色和标题颜色

  1. 白色背景+红色标题
  2. 清晰的彩色背景+白色标题

我正在使用下面的通用代码来更改背景颜色和标题颜色

@objc class Helper : NSObject{
class func restNavigationBarWithNavigationController(navigationController : UINavigationController , withColor : UIColor,isTranslucent : Bool )
{
    navigationController.navigationBar.setBackgroundImage(UIImage(), for: .default)
    navigationController.navigationBar.shadowImage = UIImage()
    navigationController.navigationBar.isTranslucent = isTranslucent
    navigationController.view.backgroundColor = withColor
    navigationController.navigationBar.backgroundColor = withColor
}
}

我在视图控制器的 viewdidload 和 ViewWIllAppear 中调用如下

self.navigationController?.navigationBar.tintColor = .white

Helper.restNavigationBarWithNavigationController(navigationController: 
self.navigationController!, withColor: .clear, isTranslucent: true)

但上面的代码不起作用如果我将屏幕从白色弹出到红色标题 & 反之亦然。

请让我知道我在这里做错了什么。

任何想法或建议都会很棒。 (仅供参考:我在 Objective-c 视图控制器中使用相同的代码库)

【问题讨论】:

  • 我想你试试这个 Helper().restNavigationBarWithNavigationController(navigationController: self.navigationController!, withColor: .clear, isTranslucent: true)
  • 这里你没有初始化你的类,首先初始化你的类,然后用那个类引用调用你的函数或者简单地做这个 Helper().restNavigationBarWithNavigationController(navigationController: self.navigationController!, withColor: .clear , isTranslucent: true)
  • 我正在从我的视图控制器 ViewDidLoad/ViewWIllAppear 调用上述类方法。请让我知道'不初始化你的课程'是什么意思
  • 据我了解,您的代码在显示带有颜色的新屏幕时运行正常,但是当您弹出该屏幕时,前一个屏幕导航栏的颜色显示不正确。如果这是您的问题,那么当该屏幕中的弹出屏幕删除您在 viewWillDisapper() 中选择的颜色时

标签: ios swift uinavigationcontroller navigation uinavigationbar


【解决方案1】:

好吧,如果你想改变每个屏幕的导航栏样式,你可以使用下面的代码:

class FirstViewController: UIViewController {

    override func viewDidLoad() {
        super.viewDidLoad()

        // White color Background + Red Color Title
        navigationController.navigationBar.barTintColor = .white
        navigationController.navigationBar.titleTextAttributes = [NSAttributedStringKey.foregroundColor: UIColor.red]
    }
}

// 下一步

class SecondViewController: UIViewController {

    override func viewDidLoad() {
        super.viewDidLoad()

        // Clear color Background + White color title
        navigationController.navigationBar.barTintColor = .clear
        navigationController.navigationBar.titleTextAttributes = [NSAttributedStringKey.foregroundColor: UIColor.white]
    }
}

这适用于 Swift 4

【讨论】:

    【解决方案2】:

    Swift 5 将NSAttributedStringKey 类型分离为NSAttributedString.Key 并按如下方式使用(Swift 5.1.3)。

    func configureNavigationBar() {
          navigationController?.navigationBar.barTintColor = .darkGray
          navigationController?.navigationBar.titleTextAttributes = [NSAttributedString.Key.foregroundColor: UIColor.white]
     }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-10-22
      • 2017-01-05
      • 2023-03-19
      • 2023-03-09
      • 2018-02-06
      相关资源
      最近更新 更多