Monday, October 1, 2012

shouldAutorotateToInterfaceOrientation not getting called in iOS 6

The shouldAutorotateToInterfaceOrientation method is deprecated in iOS6. Instead shouldAutorotate and supportedInterfaceOrientation methods are used. If some logic is used in shouldAutorotateToInterfaceOrientation method, it should be implemented in -willRotateToInterfaceOrientation:duration: method.
First, if you want the view controller’s content to be able to auto rotate just return YES in -shouldAutorotate method.
- (BOOL)shouldAutorotate
 return YES;

Supported interface orientations are returned in the -supportedInterfaceOrientations method.
- (NSUInteger)supportedInterfaceOrientations
        //if you have different views for portrait and landscape they are assigned here for the first time
 self.view = UIInterfaceOrientationIsLandscape([[UIApplication sharedApplication] statusBarOrientation]) ? _viewLandscape : _viewPortrait;
        //return orientations you want to support
 return (UIInterfaceOrientationMaskPortrait | UIInterfaceOrientationMaskLandscapeLeft | UIInterfaceOrientationMaskLandscapeRight | UIInterfaceOrientationMaskPortraitUpsideDown);

f there is some logic, like switching views for landscape and portrait on rotation use the -willRotateToInterfaceOrientation:duration: method.

- (void)willRotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation duration:(NSTimeInterval)duration
 [super willRotateToInterfaceOrientation:toInterfaceOrientation duration:duration];
 self.view = UIInterfaceOrientationIsLandscape(toInterfaceOrientation) ? _viewLandscape : _viewPortrait;

No comments:

Post a Comment