BDGapps
BDGapps

Reputation: 3356

Method to fire directly after device rotates

In my iOS app I have everything set up based on proportions. It creates all my images programmatically based on the device width and height. I found online that I need to use:

- (void)willAnimateRotationToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation
                                         duration:(NSTimeInterval)duration 

However since I use the screen width and height I need to call my reload after it rotates. What function do I use like this one that fires after the rotation occurs?

Upvotes: 1

Views: 1863

Answers (4)

Utsav Dusad
Utsav Dusad

Reputation: 2199

As of iOS 8, all rotation-related methods are deprecated. Instead, rotations are treated as a change in the size of the view controller’s view and are therefore reported using the viewWillTransitionToSize:withTransitionCoordinator: method. When the interface orientation changes, UIKit calls this method on the window’s root view controller. That view controller then notifies its child view controllers, propagating the message throughout the view controller hierarchy.

Source: iOS Developer Library

Below is the example of how to use it.

- (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.

    }];
}

Upvotes: 1

Juan Catalan
Juan Catalan

Reputation: 2309

The method viewWillLayoutSubviews is called after the interface changes orientation. A call to self.view.bounds.size will get you the correct width and height for the new orientation. Available since iOS 5.0. See UIViewController Class Reference in Apple documentation.

Upvotes: 1

Till
Till

Reputation: 27597

From the iOS developers reference on UIViewController rotation:

When a rotation occurs for a visible view controller, the willRotateToInterfaceOrientation:duration:, willAnimateRotationToInterfaceOrientation:duration:, and didRotateFromInterfaceOrientation: methods are called during the rotation. The viewWillLayoutSubviews method is also called after the view is resized and positioned by its parent. If a view controller is not visible when an orientation change occurs, then the rotation methods are never called. However, the viewWillLayoutSubviews method is called when the view becomes visible. Your implementation of this method can call the statusBarOrientation method to determine the device orientation.

So there you got all the information you need. Either use viewWillLayoutSubviews or, if that is too late for your purposes, usedidRotateFromInterfaceOrientation:.

Upvotes: 3

Mick MacCallum
Mick MacCallum

Reputation: 130193

UIDevice is capable of generating notifications to tell you about orientation changes after the fact. Just keep in mind that the enum declared for UIDeviceOrientation isn't exactly identical to the one declared for UIInterfaceOrientation.

[[UIDevice currentDevice] beginGeneratingDeviceOrientationNotifications];

[[NSNotificationCenter defaultCenter] addObserverForName:UIDeviceOrientationDidChangeNotification object:nil queue:[NSOperationQueue mainQueue] usingBlock:^(NSNotification *note) {

    UIDeviceOrientation orientation = [[UIDevice currentDevice] orientation];
    NSLog(@"%d",orientation);

}];

Upvotes: 0

Related Questions