UINavigation 标题颜色变化问题

Posted

技术标签:

【中文标题】UINavigation 标题颜色变化问题【英文标题】:UINavigation Title color change issue 【发布时间】:2018-04-30 05:10:47 【问题描述】:

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

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 以获得更好的理解。

【问题讨论】:

我认为这个链接会对你有所帮助***.com/questions/42618802/… 【参考方案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 中提供您的标题,以便在您向前导航时正确设置后退按钮。

【讨论】:

【参考方案2】:
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

【讨论】:

以上是关于UINavigation 标题颜色变化问题的主要内容,如果未能解决你的问题,请参考以下文章

应用程序的 ios 11 颜色(uinavigation)

如何更改 UINavigation leftBarButtonItem 的颜色

当 UINavigation 的 Translucent 设置为 false 时,视图约束发生变化

在 iOS 7 中为 uinavigation 栏设置色调颜色而不中断栏按钮项目 [重复]

iOS学习之UINavigationController

UIRefreshControl 在带有大标题的 UINavigation 后面呈现