Autorotation not working in iOS 7 , works fine in

2019-04-07 07:44发布

问题:

I have an app that supports landscape orientation only on some sections (Photo gallery, video, etc) and all is working fine on iOS 6 with no issues, however, in iOS 7 the app crashes.

So heres my issue :

  1. start app and load initial nav controller with view controller that only supports portrait
  2. push view controller on to stack that supports landscape AND portrait
  3. rotate view to landscape
  4. pop view controller
  5. app crashes
-->
CRASH: **preferredInterfaceOrientationForPresentation must return a supported interface orientation!**
2013-11-06 10:18:06.220 ausopen-iphone-2014[57605:70b] Stack Trace: (
  0   CoreFoundation                      0x03f665e4 __exceptionPreprocess + 180
  1   libobjc.A.dylib                     0x03ce38b6 objc_exception_throw + 44
  2   CoreFoundation                      0x03f663bb +[NSException raise:format:] + 139
  3   UIKit                               0x01366f1c -[UIViewController _preferredInterfaceOrientationForPresentationInWindow:fromInterfaceOrientation:]

+ 580

Other info :

In my info.plist i support portrait and landscape

In my AppDelegate I implement the following method :

- (NSUInteger)application:(UIApplication *)application supportedInterfaceOrientationsForWindow:(UIWindow *)window{}

In this method I specify that if the first view (HomeVC) is showing that it should support ALL orientations.

In this method I also specify that if the second view (PhotoVC) is showing that it should also support ALL orientations.

In my first view (HomeVC) i override this method with the following methods so that only portrait mode is supported when the view is showing:

- (BOOL)shouldAutorotate {
    return YES;
}

- (UIInterfaceOrientation)preferredInterfaceOrientationForPresentation
{
    return UIInterfaceOrientationPortrait;
}

- (NSUInteger)supportedInterfaceOrientations
{
    return UIInterfaceOrientationMaskPortrait;
} 

Not sure what has changed in iOS 7 in regards to this because all is working fine in iOS 6. It seems that in iOS 7 the app is not auto rotating back once the landscape view is popped.

Any help would be appreciated..

回答1:

In IOS7.If you use a UINavigationController, the rotate processing way is different!

See UINavigationController, can see it is a subclass of UIViewController, then that is in him there are listed in the above the rotation of the processing method; So wo need to use UINavigationController also do rotate processing,

My way is to add a Category to UINavigationController, do so.

In the Category. M writes

-(BOOL)shouldAutorotate {    
  return [[self.viewControllers lastObject] shouldAutorotate];
}

- (UIInterfaceOrientation)preferredInterfaceOrientationForPresentation {    
  return [[self.viewControllers lastObject] preferredInterfaceOrientationForPresentation]; 
}


回答2:

You can simple use this:--

-(void)rotateCameraView
{
    [[UIDevice currentDevice] beginGeneratingDeviceOrientationNotifications];
    [[NSNotificationCenter defaultCenter] addObserver: self selector:@selector(deviceOrientationDidChange:) name: UIDeviceOrientationDidChangeNotification object: nil];
}

//Mohit 18 Oct
- (void)deviceOrientationDidChange:(NSNotification *)notification {
    //Obtaining the current device orientation
    UIDeviceOrientation orientation = [[UIDevice currentDevice] orientation];

    //Ignoring specific orientations
    if (orientation == UIDeviceOrientationFaceUp || orientation == UIDeviceOrientationFaceDown || orientation == UIDeviceOrientationUnknown) {
        return;
    }

    if ((UIDeviceOrientationIsPortrait(orientation) ||UIDeviceOrientationIsPortrait(orientation)) ||
    (UIDeviceOrientationIsLandscape(orientation) || UIDeviceOrientationIsLandscape(orientation))) {
        //still saving the current orientation
        currentOrientation = orientation;
    }
    [self performSelector:@selector(orientationChangedMethod) withObject:nil afterDelay:0];
}

//Mohit 18 Oct
    -(void)orientationChangedMethod
    {
        switch (currentOrientation) {

            case UIDeviceOrientationPortrait:
       }
    }


//Note here current orientation is a UIDeviceOrientation currentOrientation; global variable.