Search code examples
iosswiftuiviewcontrollerios8uipresentationcontroller

With custom view controller transitions, where do I get to hide the status bar?


Sample Project: http://cl.ly/1C0N0E0f3n2P

I'm trying to create a custom transition to a view controller in iOS 8. I'm using a UIPresentationController as well as an NSObject subclass that implements UIViewControllerAnimatedTransitioning, basically following this tutorial.

I also want to hide the status bar when the view controller gets presented, but I don't know where in this paradigm I'm to do this.

Whenever I try to call methods on the presentingViewController in the UIPresentationController subclass, or using a key in the NSObject subclass, I always get a crash, making me believe I'm not supposed to be calling methods on these?

Example from sample:

class CustomPresentationController: UIPresentationController {
    override func presentationTransitionWillBegin() {
        // Following line causes crash
        (presentingViewController as ViewController).testFunction()        

        let darkOverlayView = UIView(frame: presentingViewController.view.bounds)
        darkOverlayView.backgroundColor = UIColor(white: 0.0, alpha: 0.5)

        containerView.addSubview(darkOverlayView)
    }
}

So where exactly am I to hide the status bar? I don't want to call it at the same time that I call presentViewController, as every time that gets presented I want to hide the status bar, so it should be wrapped up in the animation itself, for DRY principles.


Solution

  • The following code will fix the crash.

    let controller = presentingViewController as UINavigationController
    let ctl = controller.topViewController as ViewController
    ctl.testFunction()