UINavigationController 在转换到另一个 VC 时删除阴影

Posted

技术标签:

【中文标题】UINavigationController 在转换到另一个 VC 时删除阴影【英文标题】:UINavigationController remove drop shadow when transitioning to another VC 【发布时间】:2015-06-08 22:09:17 【问题描述】:

当使用UINavigationControllerpushViewController:animated: 将另一个视图控制器推入堆栈时,在转换过程中总是会显示一个阴影:

我有一个应用,其中导航控制器上有一个视频背景,所以我想删除那个阴影,因为它在转换过程中看起来很奇怪。

有什么办法可以彻底去除吗? (我不是在说UINavigationBarshadow)

【问题讨论】:

你解决了吗? @BorisNikolić - 不。我最终使用了UIViewController,并在那里构建了所有动画。 【参考方案1】:

解决方案:

// set one of viewcontrollers (usual first) as navigationController delegate

class ViewController: UIViewController 

    override func viewDidLoad() 
        super.viewDidLoad()

        navigationController?.delegate = self
    



// make the view controller conform to `UINavigationControllerDelegate`

extension ViewController: UINavigationControllerDelegate 
    func navigationController(_ navigationController: UINavigationController, animationControllerFor operation: UINavigationControllerOperation, from fromVC: UIViewController, to toVC: UIViewController) -> UIViewControllerAnimatedTransitioning? 
        return PushPopAnimator(operation: operation)
    


// The animation controller

class PushPopAnimator: NSObject, UIViewControllerAnimatedTransitioning 
    let operation: UINavigationControllerOperation

    init(operation: UINavigationControllerOperation) 
        self.operation = operation
        super.init()
    

    func transitionDuration(using transitionContext: UIViewControllerContextTransitioning?) -> TimeInterval 
        return 0.25
    

    func animateTransition(using transitionContext: UIViewControllerContextTransitioning) 
        let from = transitionContext.viewController(forKey: .from)!
        let to   = transitionContext.viewController(forKey: .to)!

        let rightTransform = CGAffineTransform(translationX: transitionContext.containerView.bounds.size.width, y: 0)
        let leftTransform = CGAffineTransform(translationX: -transitionContext.containerView.bounds.size.width, y: 0)

        if operation == .push 
            to.view.transform = rightTransform
            transitionContext.containerView.addSubview(to.view)
            UIView.animate(withDuration: transitionDuration(using: transitionContext), animations:  
                from.view.transform = leftTransform
                to.view.transform = .identity
            , completion:  finished in
                from.view.transform = .identity
                transitionContext.completeTransition(!transitionContext.transitionWasCancelled)
            )
         else if operation == .pop 
            to.view.transform = leftTransform
            transitionContext.containerView.insertSubview(to.view, belowSubview: from.view)
            UIView.animate(withDuration: transitionDuration(using: transitionContext), animations: 
                to.view.transform = .identity
                from.view.transform = rightTransform
            , completion:  finished in
                from.view.transform = .identity
                transitionContext.completeTransition(!transitionContext.transitionWasCancelled)
            )
        
    

【讨论】:

我知道这是一个 Swift 答案,但对于 Objective-C,键是 UITransitionContextFromViewControllerKeyUITransitionContextToViewControllerKey不要将它们与UITransitionContextToViewKey/UITransitionContextToViewKey 混淆(注意缺少的Controller)。在过去的一个小时里,这让我发疯了。 此解决方案消除了通过滑动返回导航的可能性。

以上是关于UINavigationController 在转换到另一个 VC 时删除阴影的主要内容,如果未能解决你的问题,请参考以下文章

如何在转到角度之前将 SKSpriteNode 旋转 360°

golang 在转到int8中拆分一个字符串

使 angular.forEach 在转到下一个对象后等待承诺

在转到我的应用程序的任何位置之前,如何强制用户登录?

在转到下一个视图控制器(Swift)之前进行异步调用

Nextjs如何在转到下一页时不卸载上一页(保持状态)