Interactive animation

Define a protocol:
/// Navigation bar colors for 'ColorableNavigationController', called on 'push' & 'pop' actions public protocol NavigationBarColorable: UIViewController { var navigationTintColor: UIColor? { get } var navigationBarTintColor: UIColor? { get } } public extension NavigationBarColorable { var navigationTintColor: UIColor? { return nil } }
Define a custom subclass of NavigationController :
class AppNavigationController: UINavigationController { override func viewDidLoad() { super.viewDidLoad() navigationBar.shadowImage = UIImage() if let colors = rootViewController as? NavigationBarColorable { setNavigationBarColors(colors) } } private var previousViewController: UIViewController? { guard viewControllers.count > 1 else { return nil } return viewControllers[viewControllers.count - 2] } override open func pushViewController(_ viewController: UIViewController, animated: Bool) { if let colors = viewController as? NavigationBarColorable { setNavigationBarColors(colors) } super.pushViewController(viewController, animated: animated) } override open func popViewController(animated: Bool) -> UIViewController? { if let colors = previousViewController as? NavigationBarColorable { setNavigationBarColors(colors) }
Now you can match the NavigationBarColorable in any controller inside the AppNavigationController and assign it whatever color you want.
extension FirstViewController: NavigationBarColorable { public var navigationBarTintColor: UIColor? { UIColor.red } public var navigationTintColor: UIColor? { UIColor.white } } extension SecondViewController: NavigationBarColorable { public var navigationBarTintColor: UIColor? { UIColor.blue } public var navigationTintColor: UIColor? { UIColor.orange } }
Remember to implement this useful extension:
extension UINavigationController { var rootViewController: UIViewController? { return viewControllers.first } }
Mojtaba hosseini
source share