user7684436
user7684436

Reputation: 717

Swift segue transition shadow

I'm working with a few standard segues in storyboard and they each have the same background color. The issue I'm having is that when the segue transition nears completion there appears a dark shadow like background around the whole frame.

Image showing transition

It's very faint, but enough to cause an issue. Has anyone come across this before?

Upvotes: 1

Views: 1477

Answers (1)

Rob
Rob

Reputation: 438467

The standard navigation controller push/pop animations darken the view that you're pushing from and the one you're popping to. If you don't like that, you can customize the transition, using an animation that just slides views in and out, but does no dimming of anything:

// this is the view controller you are pushing from

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

    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)
        if operation == .push {
            to.view.transform = rightTransform
            transitionContext.containerView.addSubview(to.view)
            UIView.animate(withDuration: transitionDuration(using: transitionContext), animations: { 
                to.view.transform = .identity
            }, completion: { finished in
                transitionContext.completeTransition(!transitionContext.transitionWasCancelled)
            })
        } else if operation == .pop {
            to.view.transform = .identity
            transitionContext.containerView.insertSubview(to.view, belowSubview: from.view)
            UIView.animate(withDuration: transitionDuration(using: transitionContext), animations: {
                from.view.transform = rightTransform
            }, completion: { finished in
                from.view.transform = .identity
                transitionContext.completeTransition(!transitionContext.transitionWasCancelled)
            })
        }
    }
}

For information on custom transitions with view controllers, see WWDC 2013 video Custom Transitions Using View Controllers.

Upvotes: 7

Related Questions