更新
我在 iOS 10.3 中进行了测试,我认为问题已得到解决。 transitionCordinator 不再需要。我觉得动画很流畅。请查看我的project on github 或查看此代码:
class ViewControllerA: UIViewController {
override func loadView() {
super.loadView()
title = "A"
view.backgroundColor = .white
navigationItem.rightBarButtonItem = UIBarButtonItem(title: "NEXT", style: .plain, target: self, action: #selector(self.showController))
}
override func viewWillAppear(_ animated: Bool) {
setColors()
super.viewWillAppear(animated)
}
func showController() {
navigationController?.pushViewController(ViewControllerB(), animated: true)
}
private func setColors() {
navigationController?.navigationBar.tintColor = .black
navigationController?.navigationBar.barTintColor = .red
navigationController?.navigationBar.isTranslucent = false
}
}
class ViewControllerB: UIViewController {
override func loadView() {
super.loadView()
title = "B"
view.backgroundColor = .white
}
override func viewWillAppear(_ animated: Bool) {
setColors()
super.viewWillAppear(animated)
}
override func willMove(toParentViewController parent: UIViewController?) {
if parent == nil {
navigationController?.navigationBar.barTintColor = .red
}
super.willMove(toParentViewController: parent)
}
private func setColors() {
navigationController?.navigationBar.tintColor = .black
navigationController?.navigationBar.barTintColor = .blue
navigationController?.navigationBar.isTranslucent = false
}
}
================================================ ==================================================== ==================================================== ==================================================== ==================================================== ==================================================== ===
要实现这种动画,你应该使用UIViewControllerTransitionCoordinator as Apple documentation 说它是:
采用 UIViewControllerTransitionCoordinator 协议的对象为与视图控制器转换关联的动画提供支持。(...)
所以每个UIViewController 都有自己的transitionController。要得到这个,你应该打电话给UIViewControllerClass:
self.transitionCoordinator()
来自documentation:
返回活动转换协调器对象。
所以要得到你想要的结果,你应该在 viewController transitionCoordinatior 中实现animateAlongsideTransition 方法。当您单击backButton 并向后滑动时,动画会起作用。
例子:
第一个控制器:
class ViewControllerA: UIViewController {
override func loadView() {
super.loadView()
title = "A"
view.backgroundColor = .white
navigationItem.rightBarButtonItem = UIBarButtonItem(title: "NEXT", style: .plain, target: self, action: #selector(self.showController))
setColors()
}
override func viewWillAppear(_ animated: Bool) {
super.viewWillAppear(animated)
animate()
}
func showController() {
navigationController?.pushViewController(ViewControllerB(), animated: true)
}
private func animate() {
guard let coordinator = self.transitionCoordinator else {
return
}
coordinator.animate(alongsideTransition: {
[weak self] context in
self?.setColors()
}, completion: nil)
}
private func setColors() {
navigationController?.navigationBar.tintColor = .black
navigationController?.navigationBar.barTintColor = .red
}
}
第二控制器:
class ViewControllerB : UIViewController {
override func loadView() {
super.loadView()
title = "B"
view.backgroundColor = .white
setColors()
}
override func viewWillAppear(_ animated: Bool) {
super.viewWillAppear(animated)
animate()
}
override func willMove(toParentViewController parent: UIViewController?) { // tricky part in iOS 10
navigationController?.navigationBar.barTintColor = .red //previous color
super.willMove(toParentViewController: parent)
}
override func viewDidAppear(_ animated: Bool) {
navigationController?.navigationBar.barTintColor = .blue
}
private func animate() {
guard let coordinator = self.transitionCoordinator else {
return
}
coordinator.animate(alongsideTransition: {
[weak self] context in
self?.setColors()
}, completion: nil)
}
private func setColors(){
navigationController?.navigationBar.tintColor = .black
navigationController?.navigationBar.barTintColor = .blue
}
}
更新 iOS 10
在 iOS 10 中,棘手的部分是在 second ViewController 中添加 willMoveTo(parentViewController parent: UIViewController?)。并将 navigationBar tintColor 设置为 previous 控制器的颜色值。此外,在 second ViewControler 中的 viewDidAppear 方法中,将 navigationBar.tintColor 设置为来自 second viewController 的颜色。
看看我的例子project on github