gpt4 book ai didi

ios - 为什么弹出 UIViewController 会改变 navigationItem 的 titleTextAttributes?

转载 作者:行者123 更新时间:2023-11-30 11:01:11 24 4
gpt4 key购买 nike

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

每个 View Controller 都有一个按钮,用于触发 IBAction 来实例化下一个 View Controller 。然后将该 View 推送到导航堆栈上。只要有另一个 viewController 可以推送,用户就可以执行此操作。但是当它们开始返回/弹出时我就会遇到问题。

这是 View Controller :

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,标题会正确更改颜色。

这是为什么?

最佳答案

我找到了解决此问题的解决方案...最初发布于 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]

关于ios - 为什么弹出 UIViewController 会改变 navigationItem 的 titleTextAttributes?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53343525/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com