iOS 10 barTintColor动画

时间:2016-09-27 15:21:27

标签: ios objective-c iphone animation ios10

我注意到ios 10中条形色调动画的方式发生了变化。我已经创建了一个概述更改的示例项目:Github: ios10BarTintDemo

基本上在ios 9上,barTintColor使用[UIViewControllerTransitionCoordinator animateAlongsideTransition]

平滑动画

但是在ios 10上,动画不太流畅,当弹出一个视图控制器根本没有动画时,我尝试添加[self.navigationController.navigationBar layoutIfNeeded],如同在一些类似的答案中所提到的那样,但这并不是&# 39;当推/弹控制器时似乎没有任何效果。

2 个答案:

答案 0 :(得分:13)

更新

我已在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说明:

  

采用UIViewControllerTransitionCoordinator协议的对象为与视图控制器转换关联的动画提供支持。(...)

所以每个UIViewController都有自己的transitionController。为此,您应该拨打UIViewControllerClass

self.transitionCoordinator()

来自documentation

  

返回活动的转换协调器对象。

因此,要获得您想要的结果,您应该在viewController transitionCoordinatior中实现animateAlongsideTransition方法。点击backButton并向后滑动即可使用动画。

示例:

navigation_bar_animation

第一控制器:

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设置为 上一个 控制器的颜色值。此外,在 第二 中的viewDidAppear方法中,ViewControler将navigationBar.tintColor设置为 second 的颜色strong> viewController。

查看我的示例project on github

答案 1 :(得分:5)

您可以通过添加与此类似的内容来修复此弹出式问题,在viewWillDisappear中运行它在iOS10中因某种原因而无法正常工作

override func willMove(toParentViewController parent: UIViewController?) {
    self.navigationController?.navigationBar.barTintColor = UIColor.red
    super.willMove(toParentViewController: parent)
}