18

Running on iOS 8, I need to change the UI when rotating my app.

Currently I am using this code:

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

    return YES;
}

What I do it remove the current UI and add a new UI appropriate to the orientation. However, my issue is that this method is called about 4 times every time a single rotation is made.

What is the correct way to make changes upon orientation change in iOS 8?

Pascal Cuoq
  • 79,187
  • 7
  • 161
  • 281
Josh Kahane
  • 16,765
  • 45
  • 140
  • 253
  • 6
    Why do you use this method to change interface at all? I think it's supposed just to answer the question wheter to rotate or not. Use 'willRotateToInterfaceOrientation:duration:' for iOS 7- and 'viewWillTransitionToSize:withTransitionCoordinator:' for iOS 8. – Timur Kuchkarov Oct 15 '14 at 07:57
  • Just asked a question, people trying to help but didn't bother to accept an answer or comment any detail. Sorry but that's not nice. – bisma Jun 23 '16 at 11:23

2 Answers2

26

Timur Kuchkarov is correct, but I'll post the answer since I missed his comment the first time I checked this page.

The iOS 8 method of detecting orientation change (rotation) is implementing the following method of the view controller:

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

Note: The controller's view has not yet transitioned to that size at this time, so be careful if your sizing code relies on the view's current dimensions.

Nick Merrill
  • 1,242
  • 1
  • 14
  • 20
  • Also, I just noticed better details about how to use the `coordinator` in [this answer](http://stackoverflow.com/a/24072010/1057616). – Nick Merrill Dec 30 '14 at 06:08
  • 4
    Since this method is called when the view has not yet transitioned to the new size, what method is called after the view has transitioned to the new size? – VinceFior Aug 30 '15 at 06:11
  • 1
    You should have pointed out that this method does NOT fire every time there's a rotation change. It only fires under specific circumstances of a rotation. I use this method in my code, and depending on the interface on display, sometimes it fires, sometimes it doesn't, which is why I ALSO have to include viewWillLayoutSubviews to handle all cases. – Logicsaurus Rex Sep 25 '17 at 04:41
20

The viewWillTransitionToSize:withTransitionCoordinator: method is called immediately before the view has transitioned to the new size, as Nick points out. However, the best way to run code immediately after the view has transitioned to the new size is to use a 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 the this answer for the code and to Nick for linking to it in his comment.

Community
  • 1
  • 1
VinceFior
  • 1,279
  • 13
  • 25
  • How to identify whether translation is completed? – Satyam Jul 15 '16 at 10:53
  • @Satyam The completion block denoted by the comment `// your code here` will run as soon as the transition has completed. – VinceFior Jul 16 '16 at 04:45
  • You should have pointed out that this method does NOT fire every time there's a rotation change. It only fires under specific circumstances of a rotation. I use this method in my code, and depending on the interface on display, sometimes it fires, sometimes it doesn't, which is why I ALSO have to include viewWillLayoutSubviews to handle all cases. – Logicsaurus Rex Sep 25 '17 at 04:42