【发布时间】:2019-04-19 23:26:25
【问题描述】:
我正在构建一个社交网络,允许用户通过查看他们关注的人来从一个个人资料导航到下一个个人资料。为简单起见,我创建了一个包含两个视图控制器的测试项目:ViewController 和 SecondViewController。
每个视图控制器都有一个按钮,用于触发 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