【问题标题】:Why does popping UIViewController change navigationItem's titleTextAttributes?为什么弹出UIViewController会改变navigationItem的titleTextAttributes?
【发布时间】:2019-04-19 23:26:25
【问题描述】:

我正在构建一个社交网络,允许用户通过查看他们关注的人来从一个个人资料导航到下一个个人资料。为简单起见,我创建了一个包含两个视图控制器的测试项目:ViewControllerSecondViewController

每个视图控制器都有一个按钮,用于触发 IBAction 以实例化下一个视图控制器。然后将该视图推送到导航堆栈上。只要有另一个 viewController 要推送,用户就可以这样做。但是当他们开始返回/弹出时就是我遇到问题的时候。

这里是 ViewController:

class ViewController: UIViewController {
    @IBOutlet weak var pageNumberLabel: UILabel!

    override func viewWillAppear(_ animated: Bool) {
        super.viewWillAppear(animated)

        self.navigationItem.title = "First"
        setUp()
    }

    func setUp() {
        print("Setup 1")

        let navBarAppearance = self.navigationController!.navigationBar
        navBarAppearance.setBackgroundImage(UIImage(), for: .default)
        navBarAppearance.shadowImage = UIImage()
        navBarAppearance.barStyle = .black
        navBarAppearance.isTranslucent = true
        navBarAppearance.tintColor = .white
        navBarAppearance.titleTextAttributes = [NSAttributedString.Key.foregroundColor : UIColor.white]
        self.navigationController?.view.backgroundColor = UIColor.lightGray
    }

    @IBAction func pushSecondViewController() {
        if let vc = UIStoryboard(name: "Main", bundle: nil).instantiateViewController(withIdentifier: "secondVC") as? SecondViewController {
            print("-")
            self.navigationController?.pushViewController(vc, animated: true)
        }
    }
}

这里是 SecondViewController:

class SecondViewController: UIViewController {
    @IBOutlet weak var pageNumberLabel: UILabel!

    override func viewWillAppear(_ animated: Bool) {
        super.viewWillAppear(animated)

        self.navigationItem.title = "Second"
        setUp()
    }

    func setUp() {
        print("Setup 2")

        let navBarAppearance = self.navigationController!.navigationBar
        navBarAppearance.isTranslucent = false
        navBarAppearance.barTintColor = .white
        navBarAppearance.tintColor = .blue
        navBarAppearance.barStyle = .black
        navBarAppearance.titleTextAttributes = [NSAttributedString.Key.foregroundColor : UIColor.blue]
    }

    @IBAction func pushFirstViewController() {
        if let vc = UIStoryboard(name: "Main", bundle: nil).instantiateViewController(withIdentifier: "firstVC") as? ViewController {
            print("-")
            self.navigationController?.pushViewController(vc, animated: true)
        }
    }
}

当 SecondViewController 被弹出并出现 ViewController 时,navigationTitle 保持 UIColor.blue。但是,如果我从 SecondViewController 滑动到 ViewController,标题会正确更改颜色。

这是为什么?

【问题讨论】:

标签: ios swift uinavigationcontroller uinavigationbar uinavigationitem


【解决方案1】:

我找到了解决此问题的解决方案...最初发布 here。我将以下代码放入导航栏的setUp() 函数中。

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 = [My View Name]
titleLabel.textColor = .blue
titleView.addSubview(titleLabel)
self.navigationItem.titleView = titleView
self.navigationItem.title = [My View Name]

【讨论】:

    猜你喜欢
    • 2016-09-25
    • 2018-09-18
    • 1970-01-01
    • 1970-01-01
    • 2013-03-17
    • 1970-01-01
    • 2011-05-24
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多