NavigationBar为一个视图设置TitleColor

时间:2018-10-25 12:35:48

标签: ios swift uinavigationbar swift4 navigationbar

im面临着迅速设置一个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

4 个答案:

答案 0 :(得分:1)

不是将代码添加到JLabel,而是将其添加到viewDidLoad(),即

viewDidAppear(_:)

答案 1 :(得分:1)

感谢大家的帮助。

我通过其他功能为它工作:

people.forEach(x -> {
        x.getLanguagesSpoken().forEach(lang -> {
            langPersons.computeIfAbsent(lang, ignoreMe -> new ArrayList<>()).add(x);
        });
});

建立视图时也会调用此函数。 此解决方案对我有效,显示视图时已经设置了颜色。
我愿意接受对此的回应。

答案 2 :(得分:0)

您可以在任何视图的代码中使用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")
 }

答案 3 :(得分:-1)

子类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]
    }
}