Swift: Previous view rotated while fading to a view that has a different orientation

61 Views Asked by At

My previous view (CaptureVC) doesn't autorotate, so it only has a portrait orientation. My second view that it transitions into (ManageCaptureVC) does autorotate, and while it is doing a fading transition into it, CaptureVC is suddenly rotated while fading if the device is in landscape. How can this be avoided or compensated for?

To be more thorough, it is a scaling animation, then a fading animation into the new view controller (ManageCaptureVC):

UIView.animate(withDuration: 0.3, delay: 0.0, options: UIViewAnimationOptions.curveEaseOut, animations: {
    self.view.transform = CGAffineTransform(scaleX: 0.75, y: 0.75)
}, completion: { (finished: Bool) in
    // Here is where CaptureVC rotates while fading into ManageCaptureVC
    let transition = CATransition()
    transition.duration = 0.5
    transition.type = kCATransitionFade
    transition.timingFunction = CAMediaTimingFunction(name:kCAMediaTimingFunctionEaseIn)
    self.present(manageCaptureVC, animated: false, completion: nil)
})

In my app delegate, I overrode these two functions:

func application(_ application: UIApplication, supportedInterfaceOrientationsFor window: UIWindow?) -> UIInterfaceOrientationMask {
    if let rootViewController = self.topViewControllerWithRootViewController(rootViewController: window?.rootViewController) {
        if (rootViewController.responds(to: Selector(("canRotate")))) {
            // Unlock landscape view orientations for this view controller if it is not currently being dismissed
            if !rootViewController.isBeingDismissed{
                return .all
            }
        }
    }

    // Only allow portrait (standard behaviour)
    return .portrait
}

private func topViewControllerWithRootViewController(rootViewController: UIViewController!) -> UIViewController? {
    if (rootViewController == nil) {
        return nil
    }
    if (rootViewController.isKind(of: UITabBarController.self)) {
        return topViewControllerWithRootViewController(rootViewController: (rootViewController as! UITabBarController).selectedViewController)
    } else if (rootViewController.isKind(of: UINavigationController.self)) {
        return topViewControllerWithRootViewController(rootViewController: (rootViewController as! UINavigationController).visibleViewController)
    } else if (rootViewController.presentedViewController != nil) {
        return topViewControllerWithRootViewController(rootViewController: rootViewController.presentedViewController)
    }
    return rootViewController
}

And ManageCaptureVC I added:

@objc func canRotate(){}

So that it so it responds to autorotating.

0

There are 0 best solutions below