自定义过渡中的动画导航栏颜色

问题描述 投票:1回答:1

我正在使用自定义过渡进行模式演示(iOS 12-13)。我在黑暗模式下有问题。第一个带有黑色背景色的控制器不可见。

My app

如何通过动画更改导航栏颜色?

类似于《提醒》(iOS 13):

Reminders iOS 13

swift uinavigationcontroller uinavigationbar
1个回答
0
投票

我解决了我的问题。我将仅对iOS 12使用自定义过渡。

我的动画代码的一部分(用于演示):

func animateTransition(using transitionContext: UIViewControllerContextTransitioning) {
        let container       = transitionContext.containerView
        let fromController  = transitionContext.viewController(forKey: .from)!
        let toController    = transitionContext.viewController(forKey: .to)!
        let originalFrame   = transitionContext.finalFrame(for: toController)
        let toView          = transitionContext.view(forKey: .to)!

        let fromView = transitionContext.viewController(forKey: .from)!

        container.addSubview(toView)

        let offset = SlideTransitionHelper.offset(controllerSize: originalFrame.size)
        toView.frame = originalFrame.offsetBy(dx: offset.x, dy: offset.y)
        let scale: CGFloat = UIApplication.shared.statusBarFrame.height == 20 ? 0.93 : 0.91

        UIView.animate(
            withDuration: self.transitionDuration(using: transitionContext),
            delay: 0,
            usingSpringWithDamping: 1,
            initialSpringVelocity: 0.4,
            options: .allowUserInteraction,
            animations: {
                toView.frame = originalFrame
                toView.roundCorners([.topLeft, .topRight], radius: 12)
                toView.layer.masksToBounds = true

                if let tabBarController = fromController as? UITabBarController {
                    if let activeController = tabBarController.selectedViewController as? UINavigationController, let childController = activeController.children.first{

                        //Animate NavigationBar color
                        childController.navigationController?.navigationBar.barTintColor = self.themeManager.current.systemSecondaryColor
                        childController.navigationController?.navigationBar.layoutIfNeeded()

                        //Animate controller background color
                        childController.view.backgroundColor = self.themeManager.current.systemSecondaryColor

                    }
                }

                //Scale controller
                var transform = CATransform3DIdentity
                transform = CATransform3DScale(transform, scale, scale, 1.0)
                transform = CATransform3DTranslate(transform, 0, scale, 0)
                fromView.view.layer.transform = transform
                fromView.view.layer.cornerRadius = 12
                fromView.view.layer.masksToBounds = true
        },
            completion: { (isFinished) in
            transitionContext.completeTransition(isFinished)
        })
    }
© www.soinside.com 2019 - 2024. All rights reserved.