UINAvigation标题颜色变更问题



我在弹出控制器时面对导航标题颜色不会改变。请找到以下代码。

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)
    }

我对标题的颜色更改有一个面对面的问题,请检查以下视频以更好地理解。

这似乎是一个尴尬的问题,在该问题中,导航栏的标题属性仅由前进。

尝试了一切后,我可以解决以下解决方案,在此提供了我自己的标题视图。

    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中提供标题,以便在向前导航时正确设置了返回按钮。

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

最新更新