Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/ios/110.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
在iOS10中设置导航栏颜色更改动画不工作_Ios_Swift2_Ios10 - Fatal编程技术网

在iOS10中设置导航栏颜色更改动画不工作

在iOS10中设置导航栏颜色更改动画不工作,ios,swift2,ios10,Ios,Swift2,Ios10,我升级到XCode 8.0/iOS 10,现在导航栏的颜色更改动画不再工作,它直接更改颜色,而无需任何动画 UIView.animateWithDuration(0.2, animations: { self.navigationController?.navigationBar.barTintColor = currentSection.color! }) 有人知道如何解决这个问题吗?要在iOS10中为导航栏的颜色更改设置动画,需要在动画块内设置颜色后调用layoutIfNeeded

我升级到XCode 8.0/iOS 10,现在导航栏的颜色更改动画不再工作,它直接更改颜色,而无需任何动画

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

有人知道如何解决这个问题吗?

要在iOS10中为导航栏的颜色更改设置动画,需要在动画块内设置颜色后调用
layoutIfNeeded

示例代码:

UIView.animateWithDuration(0.5) { 
    self.navigationController?.navigationBar.barTintColor = UIColor.redColor()
    self.navigationController?.navigationBar.layoutIfNeeded()
}
我还想告诉大家,苹果动画的属性,比如barTintColor,所以这个方法可以随时中断

如果在动画期间在导航栏上调用-layoutifneed 块,它应该更新其背景属性,但鉴于 关于这些属性的作用,实际上从来没有任何 保证您可以为其中任何一个设置动画

交互式动画

定义协议:

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

它确实有效,但我的导航栏的标题也在设置动画(我没有在“animateWithDuration”中设置标题)功能。当颜色更改为动画时,标题从左上角移动到导航栏的中心…@Tiois,我认为问题只存在于您的项目中。如果您在相同的布局周期中设置标题,请检查您在ViewController中更改的所有属性。我已创建了测试项目,标题保持不变,您可以在ere:你是对的,在设置标题后,我现在在导航上调用layoutIfNeeded(),然后我用layoutIfNeeded()调用UIView.animateWithDuration函数,它工作得很好!谢谢。你刚刚救了我一天
layoutIfNeeded()
工作得很好,谢谢你救了我一天。:)这是有史以来最好的答案!谢谢