弹出控制器时,我面对的导航标题颜色没有改变。请找到下面的代码。

ProfilescreenVC.swift
override func viewWillAppear(_ animated: Bool) {
        super.viewWillAppear(animated)
        self.navigationController?.navigationBar.tintColor = UIColor.white
        self.navigationController?.navigationBar.barTintColor = UIColor.primaryGreen
        let textAttributes = [NSAttributedStringKey.foregroundColor:UIColor.white]
        self.navigationController?.navigationBar.titleTextAttributes = textAttributes
    }

EditprofileVC.swift
override func viewDidLoad() {
        super.viewDidLoad()
        self.style()
    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }

    func style() {
        navigationController?.navigationBar.tintColor = UIColor.black
        navigationController?.navigationBar.barTintColor = UIColor.white
        let textAttributes = [NSAttributedStringKey.foregroundColor:UIColor.black]
        navigationController?.navigationBar.titleTextAttributes = textAttributes

        carbonTabSwipeNavigation.setIndicatorColor(UIColor.black)
        carbonTabSwipeNavigation.setTabBarHeight(50.0)
        carbonTabSwipeNavigation.setNormalColor(UIColor.hexString("9A9CA1"), font: UIFont.systemFont(ofSize: 15))
        carbonTabSwipeNavigation.setSelectedColor(UIColor.black, font: UIFont.systemFont(ofSize: 15))

        self.navigationController?.view.setNeedsLayout()
        self.navigationController?.view.layoutIfNeeded()
    }

    override func viewDidDisappear(_ animated: Bool) {
        navigationController?.navigationBar.tintColor = UIColor.white
        navigationController?.navigationBar.barTintColor = UIColor.white
        let textAttributes = [NSAttributedStringKey.foregroundColor:UIColor.white]
        navigationController?.navigationBar.titleTextAttributes = textAttributes
        super.viewDidDisappear(animated)
    }

我遇到标题颜色变化的问题,请检查以下video以获得更好的理解。

最佳答案

这似乎是一个尴尬的问题,其中导航栏的title属性仅向前应用。

在尝试了所有方法之后,我可以解决以下变通办法,其中在NavigationItem中提供了自己的titleView。

    let titleView = UIView(frame: CGRect(x: 0, y: 0, width: 100, height: 50))
    titleView.backgroundColor = UIColor.clear
    let titleLabel = UILabel(frame: CGRect(x: 0, y: 0, width: 100, height: 50))
    titleLabel.textAlignment = .center
    titleLabel.text = "Profile"
    titleLabel.textColor = .white
    titleView.addSubview(titleLabel)
    self.navigationItem.titleView = titleView
    self.navigationItem.title = "Profile"

请注意,您还需要在navigationItem.title中提供标题,以便在向前导航时正确设置后退按钮。

关于ios - 导航标题颜色更改问题,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/50094290/

10-14 17:04
查看更多