【问题标题】:NavigationBar set TitleColor for one ViewNavigationBar 为一个 View 设置 TitleColor
【发布时间】:2019-03-30 01:48:51
【问题描述】:

我面临的问题是在 swift 中设置一个 ViewController 的标题的字体颜色并在它消失时重置它。目前我可以将颜色从黑色设置为白色:

override func viewDidLoad() {
  let textAttributes = [NSAttributedStringKey.foregroundColor:UIColor.white]
  navigationController?.navigationBar.titleTextAttributes = textAttributes
}


override func viewWillDisappear(_ animated: Bool) {
  let textAttributes = [NSAttributedStringKey.foregroundColor:UIColor.black]
  navigationController?.navigationBar.titleTextAttributes = textAttributes
}

当我尝试使用 UIColor.black 进行重置时,它不会改变任何东西。

当我尝试设置整个外观时,根本没有任何变化。

override func viewDidLoad() {
  UINavigationBar.appearance().titleTextAttributes = [NSAttributedStringKey.foregroundColor : UIColor.white]
}


override func viewWillDisappear(_ animated: Bool) {
  UINavigationBar.appearance().titleTextAttributes = [NSAttributedStringKey.foregroundColor : UIColor.black]
}

我怎样才能做到这一点? 使用 Xcode 10.0 斯威夫特 4

【问题讨论】:

  • 你好菲利克斯!那么,您具体要更改哪个视图的颜色?你可以再详细一点吗?或者甚至发布一张图片,这会很有帮助
  • 对不起,我忘了提这个。我正在尝试在 UINavigationBar 中设置标题的颜色。这种颜色只能在一个特定的 ViewController 中使用
  • 另一个问题:你在使用故事板吗?如果是,您是否在 .storyboard 中设置了任何色调或相关内容?
  • 我正在使用情节提要并在代码中设置所有颜色。在带有白色标题的 ViewController 中,我需要从 NavigationBar 中删除所有背景(ShadowImage、BackgroundImage),因为我需要非常特定的颜色。
  • 从 viewController 生命周期中重写方法时,不要忘记始终调用超级方法。

标签: ios swift uinavigationbar swift4 navigationbar


【解决方案1】:

不是将代码添加到viewDidLoad(),而是将其添加到viewDidAppear(_:),即

override func viewDidAppear(_ animated: Bool) {
    super.viewDidAppear(animated)
    let textAttributes = [NSAttributedString.Key.foregroundColor:UIColor.white]
    navigationController?.navigationBar.titleTextAttributes = textAttributes
}

override func viewWillDisappear(_ animated: Bool) {
    super.viewWillDisappear(animated)
    let textAttributes = [NSAttributedString.Key.foregroundColor:UIColor.black]
    navigationController?.navigationBar.titleTextAttributes = textAttributes
}

【讨论】:

  • 这并没有改变任何东西。我刚刚尝试将更改为黑色添加到对我有用的 viewDidAppear(:) 中的前一个 ViewController 中。它在 viewWillAppear(:) 中不起作用
  • 完全正确..它不会出现在viewWillAppear
【解决方案2】:

感谢大家的帮助。

我用另一个功能让它为我工作:

override func willMove(toParentViewController parent: UIViewController?) {
    var textAttributes: [NSAttributedString.Key : Any]?
    if parent == nil{ // navigating back
        textAttributes = [NSAttributedStringKey.foregroundColor:UIColor.black]
    }else{
         textAttributes = [NSAttributedStringKey.foregroundColor:UIColor.white]
    }
    navigationController?.navigationBar.titleTextAttributes = textAttributes
}

在构建视图时也会调用此函数。 该解决方案对我有用,并且在显示视图时已经设置了颜色。
我愿意接受赞成/反对的回应。

【讨论】:

    【解决方案3】:

    您可以在任何视图的代码中使用此函数 viewWillAppear

    func navigationBarProperties(vc:UIViewController, title:String){
     vc.navigationController!.navigationBar.isHidden = false
     vc.navigationController!.navigationBar.isTranslucent = false
     // text color
     vc.navigationController!.navigationBar.tintColor = UIColor.white
     // bar color
     vc.navigationController!.navigationBar.barTintColor = UIColor.black
     let isFont = UIFont.init(name: "Helvetica-bold", size: 15)
     vc.navigationItem.title = title
     vc.navigationController!.navigationBar.titleTextAttributes = [NSAttributedStringKey.foregroundColor: #colorLiteral(red: 1, green: 1, blue: 1, alpha: 1) ,NSAttributedStringKey.font: isFont!]
    }
    
    override open func viewWillAppear(_ animated: Bool) {
     super.viewWillAppear(animated)
     //---nav bar customization----//
     navigationBarProperties(vc: self, title: "Home")
     }
    

    【讨论】:

      【解决方案4】:

      子类UINavigationController 并将其分配给您的navigationController:

      class CustomNavigationController : UINavigationController {
      
          let usualTextAttributes: [NSAttributedString.Key: Any] = [.foregroundColor: UIColor.red]
          let customTextAttributes: [NSAttributedString.Key: Any] = [.foregroundColor: UIColor.blue]
      
          private func updateTitleColorIfNeeded() {
              if topViewController is MyCustomViewController {
                  navigationBar.titleTextAttributes = customTextAttributes
              } else {
                  navigationBar.titleTextAttributes = usualTextAttributes
              }
          }
      
          override func popViewController(animated: Bool) -> UIViewController? {
              updateTitleColorIfNeeded()
              return super.popViewController(animated: animated)
          }
      
          override func pushViewController(_ viewController: UIViewController, animated: Bool) {
              updateTitleColorIfNeeded()
              super.pushViewController(viewController, animated: animated)
          }
      }
      

      如果MyCustomViewController是导航控制器的根,那么在viewDidLoad中设置它的初始标题颜色:

      class MyCustomViewController: UIViewController {
          override func viewDidLoad() {
              super.viewDidLoad()
              navigationController?.navigationBar.titleTextAttributes = [.foregroundColor: UIColor.yellow]
          }
      }
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2022-01-27
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2020-02-18
        • 1970-01-01
        相关资源
        最近更新 更多