iOS 10 barTintColor 动画
iOS 10 barTintColor animation
我注意到 ios10 中条形色调颜色动画的方式发生了变化。我创建了一个示例项目来概述变化:Github: ios10BarTintDemo
基本上在 ios 9 上,barTintColor 使用 [UIViewControllerTransitionCoordinator animateAlongsideTransition]
平滑地制作动画
但是在 ios 10 上,动画不太流畅,当弹出视图控制器时根本没有动画,我尝试添加 [self.navigationController.navigationBar layoutIfNeeded]
,如一些类似的答案中所述,但这pushing/popping 控制器时似乎没有任何效果。
您可以通过添加与此类似的内容来解决此弹出问题,运行 它在 viewWillDisappear 中由于某些原因在 iOS10
中不起作用
override func willMove(toParentViewController parent: UIViewController?) {
self.navigationController?.navigationBar.barTintColor = UIColor.red
super.willMove(toParentViewController: parent)
}
更新
我在 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
作为 Apple documentation 说它是 :
An object that adopts the UIViewControllerTransitionCoordinator protocol provides support for animations associated with a view controller transition.(...)
所以每个 UIViewController
都有自己的 transitionController
。为此,您应该调用 UIViewControllerClass
:
self.transitionCoordinator()
Returns the active transition coordinator object.
因此,要获得您想要的结果,您应该在 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
}
}
更新iOS10
在 iOS 10 中棘手的部分是在 second ViewController。并将 navigationBar tintColor
设置为 previous 控制器的颜色值。此外,在 second ViewControler 的 viewDidAppear
方法中,将 navigationBar.tintColor
的颜色设置为 秒viewController.
看看我的例子project on github
我注意到 ios10 中条形色调颜色动画的方式发生了变化。我创建了一个示例项目来概述变化:Github: ios10BarTintDemo
基本上在 ios 9 上,barTintColor 使用 [UIViewControllerTransitionCoordinator animateAlongsideTransition]
但是在 ios 10 上,动画不太流畅,当弹出视图控制器时根本没有动画,我尝试添加 [self.navigationController.navigationBar layoutIfNeeded]
,如一些类似的答案中所述,但这pushing/popping 控制器时似乎没有任何效果。
您可以通过添加与此类似的内容来解决此弹出问题,运行 它在 viewWillDisappear 中由于某些原因在 iOS10
中不起作用override func willMove(toParentViewController parent: UIViewController?) {
self.navigationController?.navigationBar.barTintColor = UIColor.red
super.willMove(toParentViewController: parent)
}
更新
我在 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
作为 Apple documentation 说它是 :
An object that adopts the UIViewControllerTransitionCoordinator protocol provides support for animations associated with a view controller transition.(...)
所以每个 UIViewController
都有自己的 transitionController
。为此,您应该调用 UIViewControllerClass
:
self.transitionCoordinator()
Returns the active transition coordinator object.
因此,要获得您想要的结果,您应该在 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
}
}
更新iOS10
在 iOS 10 中棘手的部分是在 second ViewController。并将 navigationBar tintColor
设置为 previous 控制器的颜色值。此外,在 second ViewControler 的 viewDidAppear
方法中,将 navigationBar.tintColor
的颜色设置为 秒viewController.
看看我的例子project on github