Swift 4 Expression type '@value CGRect' is ambiguous without more context

CoderQueue picture CoderQueue · Jul 2, 2018 · Viewed 34k times · Source

I am trying to implement a animation for a rectangle to perform modal vertical swipe.However, when I try to compile the code I get the following error "Swift 4 Expression type '@value CGRect' is ambiguous without more context". I have isolated the issue to the arguments that is being passed to the CGRect init value, but according the Apple's iOS documentation these parameters should be enough to specify the 'frame' view that I need to animate.

Here is my code:

func animateTransition(using transitionContext: UIViewControllerContextTransitioning) {
    guard
        let fromView = transitionContext.viewController(forKey: .from),
        let toView = transitionContext.viewController(forKey: .to)

    else {
            return
    }
    let containerView = transitionContext.containerView
    containerView.insertSubview((toView.view)!, belowSubview: (fromView.view)!)

    let toFrame = transitionContext.finalFrame(for: toView)

    let screenBounds = UIScreen.main.bounds

    let bottomLeftCorner = CGPoint(x: 0, y: screenBounds.height)

    let finalFrameForVC = CGRect(origin: bottomLeftCorner, size: screenBounds.size)

    UIView.animate(withDuration: 2, delay: 1,
                   options: [], (using: transitionContext.finalFrame(for: fromView)),
        animations: {
            fromView.view.frame = finalFrameForVC
    },
        completion: { _ in
            transitionContext.completeTransition(!transitionContext.transitionWasCancelled)
    })
}

Answer

iainhunter picture iainhunter · Sep 24, 2018

I had the same error when I tried to multiply view.frame.height by a Double. If you are doing any math on screenBounds properties, make sure the types match.