我注意到ios 10中条形图颜色动画的方式发生了变化。
基本上,在ios 9上,barTintColor使用[UIViewControllerTransitionCoordinator animateAlongsideTransition]平稳地动画。
但是在ios 10上,动画就不那么流畅了,当弹出一个视图控制器时,我尝试了添加一些类似的答案中提到的[self.navigationController.navigationBar layoutIfNeeded],但是在推送/弹出控制器时,这似乎没有任何效果。
发布于 2016-10-26 22:14:27
更新
我在iOS 10.3中进行了测试,我认为问题已经解决了。transitionCordinator也不再需要了。我觉得动画很流畅。请查看我的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,就像苹果文档说的那样:
采用UIViewControllerTransitionCoordinator协议的对象提供对与视图控制器转换相关联的动画的支持。
所以每个UIViewController都有自己的transitionController。要获得这个结果,您应该调用UIViewControllerClass:
self.transitionCoordinator()
来自文档
返回活动转换协调器对象。
因此,要获得您想要的结果,您应该在viewController transitionCoordinatior中实现viewController方法。当您单击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中,棘手的部分是在第二个 ViewController中添加willMoveTo(parentViewController parent: UIViewController?)。并将navigationBar tintColor设置为以前的控制器的颜色值。此外,在viewDidAppear方法中,第二次 ViewControler将navigationBar.tintColor设置为来自第二 viewController的颜色。
发布于 2016-10-10 21:49:03
您可以通过添加类似于此的内容来解决这个弹出问题,在viewWillDisappear中运行它在iOS10中由于某种原因无法工作。
override func willMove(toParentViewController parent: UIViewController?) {
self.navigationController?.navigationBar.barTintColor = UIColor.red
super.willMove(toParentViewController: parent)
}https://stackoverflow.com/questions/39728460
复制相似问题