UINavigation标题颜色变化问题

时间:2018-04-30 05:10:48

标签: ios uinavigationcontroller uinavigationbar

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

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以便更好地理解。

2 个答案:

答案 0 :(得分:1)

这似乎是一个尴尬的问题,导航栏的标题属性只能通过前进来应用。

在尝试了所有内容后,我可以解决以下解决方法,其中我在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中提供标题,以便在向前导航时正确设置后退按钮。

答案 1 :(得分:0)

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

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

无需在代码中添加ViewDidDisapper