swift2 iOS10中的动画导航栏颜色更改不起作用

8yoxcaq7  于 2022-11-06  发布在  Swift
关注(0)|答案(2)|浏览(187)

我升级到了XCode 8.0 / iOS 10,现在我的导航栏的颜色变化动画不再工作了,它直接改变颜色,没有任何动画。

UIView.animateWithDuration(0.2, animations: {
    self.navigationController?.navigationBar.barTintColor = currentSection.color!
})

有人知道怎么解决吗?

fnx2tebb

fnx2tebb1#

要在iOS10中设置navigationBar的颜色变化动画,您需要在动画块内设置颜色后调用layoutIfNeeded
示例代码:

UIView.animateWithDuration(0.5) { 
    self.navigationController?.navigationBar.barTintColor = UIColor.redColor()
    self.navigationController?.navigationBar.layoutIfNeeded()
}

我还想通知苹果doesn’t officialy support动画在这样的属性,如barTintColor,所以该方法可以在任何时候打破.
如果您在动画块期间调用导航栏上的-layoutIfNeeded,它应该更新其背景属性,但鉴于这些属性的性质,实际上还没有任何类型的保证,您可以动画其中任何一个。

j2qf4p5b

j2qf4p5b2#

交互式动画

定义协议:

/// 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 }
}

定义自定义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)
        }

        // Let's start pop action or we can't get transitionCoordinator()
        let popViewController = super.popViewController(animated: animated)

        // Secure situation if user cancelled transition
        transitionCoordinator?.animate(alongsideTransition: nil, completion: { [weak self] context in
            guard let `self` = self else { return }

            guard let colors = self.topViewController as? NavigationBarColorable else { return }
            self.setNavigationBarColors(colors)
        })

        return popViewController
    }

    override func popToRootViewController(animated: Bool) -> [UIViewController]? {
        if let colors = rootViewController as? NavigationBarColorable {
            setNavigationBarColors(colors)
        }

        let controllers = super.popToRootViewController(animated: animated)

        return controllers
    }

    private func setNavigationBarColors(_ colors: NavigationBarColorable) {

        if let tintColor = colors.navigationTintColor {
            navigationBar.titleTextAttributes = [
                .foregroundColor : tintColor
            ]
            navigationBar.tintColor = tintColor
        }

        navigationBar.barTintColor = colors.navigationBarTintColor
    }
}

现在,您可以在AppNavigationController内的任何控制器中符合NavigationBarColorable,并为它提供任何您想要的颜色。

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 }
}

不要忘记实现这个有用的扩展:

extension UINavigationController {
    var rootViewController: UIViewController? {
        return viewControllers.first
    }
}

相关问题