IOS 8 orientation change detection

Running on iOS 8, I need to change the interface when I rotate my application.

I am currently using this code:

-(BOOL)shouldAutorotate { UIDeviceOrientation orientation = [[UIDevice currentDevice] orientation]; if (orientation != UIInterfaceOrientationUnknown) [self resetTabBar]; return YES; } 

What I do is delete the current user interface and add a new user interface that matches the orientation. However, my problem is that this method is called about 4 times every time one rotation is made.

What is the correct way to make changes when changing orientation in iOS 8?

+17
objective-c ios8 orientation
Oct 11 '14 at 13:10
source share
2 answers

Timur Kuchkarov is right, but I will send an answer, since I missed his comment when I first checked this page.

The method for determining iOS 8 detecting changes in orientation (rotation) implements the following controller method of the form:

 - (void)viewWillTransitionToSize:(CGSize)size withTransitionCoordinator:(id<UIViewControllerTransitionCoordinator>)coordinator { // Do view manipulation here. [super viewWillTransitionToSize:size withTransitionCoordinator:coordinator]; } 

Note. Currently, the controller view has not yet moved to this size, so be careful if your dimensional code depends on the current size of the view.

+26
Dec 30 '15 at 5:50
source share

The viewWillTransitionToSize:withTransitionCoordinator: called just before the view has viewWillTransitionToSize:withTransitionCoordinator: , as Nick points out. However, the best way to run the code immediately after moving to a new size is to use the completion block in the method:

 - (void)viewWillTransitionToSize:(CGSize)size withTransitionCoordinator:(id<UIViewControllerTransitionCoordinator>)coordinator { [coordinator animateAlongsideTransition:nil completion:^(id<UIViewControllerTransitionCoordinatorContext> context) { // your code here }]; } 

Thanks to this answer for the code and Nick for linking to it in his comment.

+20
Aug 30 '15 at 6:21
source share



All Articles