Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is there a non-deprecated method that matches didRotateFromInterfaceOrientation

I'm trying to find a method call I can latch onto once a device's orientation has changed. Is there something identical to didRotateFromInterfaceOrientation that isn't deprecated?

like image 604
Brandon Avatar asked Aug 06 '15 20:08

Brandon


2 Answers

As of iOS 8, all UIViewControllers inherit the UIContentContainer protocol, one of whose methods is - (void)viewWillTransitionToSize:(CGSize)size withTransitionCoordinator:(id <UIViewControllerTransitionCoordinator>)coordinator, which you can (simplistically) override like this:

- (void)viewWillTransitionToSize:(CGSize)size withTransitionCoordinator:(id <UIViewControllerTransitionCoordinator>)coordinator
{
    [super viewWillTransitionToSize:size withTransitionCoordinator:coordinator];

    [coordinator animateAlongsideTransition:^(id<UIViewControllerTransitionCoordinatorContext> context) {

        // Stuff you used to do in willRotateToInterfaceOrientation would go here.
        // If you don't need anything special, you can set this block to nil.

    } completion:^(id<UIViewControllerTransitionCoordinatorContext> context) {

        // Stuff you used to do in didRotateFromInterfaceOrientation would go here.
        // If not needed, set to nil.

    }];
}

You'll notice there's nothing specific about orientation, which is by design; iOS view rotations are essentially a composition of a specific translation matrix operation (resizing a view from one aspect ratio to another is just a specific case of a general resizing operation where the source and target view sizes are known in advance) and a rotation matrix operation (which is handled by the OS).

like image 80
fullofsquirrels Avatar answered Nov 15 '22 14:11

fullofsquirrels


Swift 3 version of fullofsquirrels's answer:

override func viewWillTransition(to size: CGSize, with coordinator: UIViewControllerTransitionCoordinator) {
    coordinator.animate(alongsideTransition: { context in
            context.viewController(forKey: UITransitionContextViewControllerKey.from)
            // Stuff you used to do in willRotateToInterfaceOrientation would go here.
            // If you don't need anything special, you can set this block to nil. 
        }, completion: { context in
            // Stuff you used to do in didRotateFromInterfaceOrientation would go here.
            // If not needed, set to nil.
        })
}
like image 39
Mohit Singh Avatar answered Nov 15 '22 14:11

Mohit Singh