简体   繁体   中英

UIViewController custom transition stuck on iOS13

I've implemented a custom transition between two view controllers in my iOS app and it worked fine with iOS 10, 11, and 12.

Now I want make it ready for iOS 13 using Xcode 11 beta 6 and iOS 13 beta 8, but the transition is stuck.

The custom transition should move the first view controller up and out of the screen and the second one from bottom up. But now it falls back to iOS13 default presentation style pageSheet , just scales the first view controller down a little bit and adds a dimmed overlay. But the second view doesn't appear.

I've found that in the method animatePresentation(context: UIViewControllerContextTransitioning) the context doesn't return a 'from' view, so context.view(forKey: .from) returns nil .

What am I supposed to do without a 'from' view?

FlyUpTransition.swift

class FlyUpTransition: NSObject, UIViewControllerAnimatedTransitioning {

    var mode: Mode = .present

    enum Mode {
        case present
        case dismiss
    }

    func transitionDuration(using transitionContext: UIViewControllerContextTransitioning?) -> TimeInterval {
        return TimeInterval(0.45)
    }

    func animateTransition(using transitionContext: UIViewControllerContextTransitioning) {
        switch mode {
        case .present:
            animatePresentation(context: transitionContext)
        case .dismiss:
            animateDismissal(context: transitionContext)
        }
    }

    func animatePresentation(context: UIViewControllerContextTransitioning) {
        guard let fromView = context.view(forKey: .from), let toView = context.view(forKey: .to) else { return }
        ...
    }

    func animateDismissal(context: UIViewControllerContextTransitioning) {
        guard let fromView = context.view(forKey: .from), let toView = context.view(forKey: .to) else { return }
        ...
    }
}

TL;DR

This is a bug in iOS, but you can use context.viewController(forKey:.from).view as a workaround.

Full Details

This appears to be a bug in iOS 13.0. As of iOS 13.1 beta 3, it's still there as well. http://www.openradar.me/radar?id=4999313432248320

The transition context's view(forKey:) method is incorrectly returning nil under certain circumstances. It appears this is happening for view(forKey: .from) when the presenting view controller is being presented non-modally. When dismissing a view controller that was originally presented from a non-modal view controller, the result of view(forKey: .to) is also nil.

I've observed this not only on the new sheet-style presentation on iPhone, but also in normal form sheets and popovers on iPad.

Another manifestation of this issue seems to be that the finalFrame(for:) method returns an incorrect CGRect when asked what the final frame for this view controller should be. In my testing, it is returning a full-screen rect, even though the correct view is smaller.

A workaround is to use the root view controller of view controller returned by viewController(forKey:) method, although the documentation explicitly discourages that: "The view returned by this method may or may not be the root view of the corresponding view controller."

Ok, it was easy, even though, it's a breaking API change of Apple.

viewController.modalPresentationStyle = .fullScreen

Now I have to go through my whole project and check all modal presentations if they still look as I need them to.

上面的答案是将modalPresentationStyle设置为.fullScreen是正确的,但是还值得一提的是,如果您的视图控制器嵌入在UINavigationController ,则需要在导航控制器上设置它:

navigationController.modalPresentationStyle = .fullScreen

I set up the segue in IB by drag-and-drop from a collection VC to another VC, which is used to display details.

I have a new discoveries on this problem, To refer to 'toView' and 'fromView', both of the following methods work

Indirectly way:

transitionContext.viewController(forKey: .to)?.view
transitionContext.viewController(forKey: .from)?.view

Directly way:

transitionContext.view(forKey: .to)
transitionContext.view(forKey: .from)

But when I switched the segue style to 'Over Full Screen', the directly way return 'nil' for both 'toView' and 'fromView' and only indirectly way work.

Hope this will be helpful to someone in the future.

PS This is my discovery along my way to solve another problem , which might be helpful, if you also have encounter the problem that 'a working animator' stops working in iOS 13 and above

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM