shouldAutorotate behavior in iOS 8

前端 未结 1 702
挽巷
挽巷 2021-01-02 13:33

I found a small behavior change between 7.1 and 8 on the UIViewController shouldAutorotate method. The Apple View Controller Programming Guide states that T

相关标签:
1条回答
  • 2021-01-02 13:38

    Same thing was happening to me. I have a feeling there is a bug in iOS8 because my same code worked fine in iOS7. Also just as a user I was have a lot of rotate problems in the apps I use. In 8 shouldAutorotate calls when you rotate to the side but then not again when you return to a normal rotation. That is unless it actually did an autorotation to the side, in which case on the return it is called.

    So it's because your `shouldAutorotate' is always returning 'NO'. My assumption is that for some reason in iOS8 they (the apple coders who changed this behavior) decided that if they got a NO when rotating to the side, they don't need to call shouldAutorotate when rotating back to portrait. So it broke the behavior that we were expecting.

    No one is talking about it or complaining. Google returns almost no results for this exact scenario. I think that's because in order for this to happen you have to be trying to use 'shouldAutorotate' as a device rotation notification but NOT actually be using the OS's auto rotation features. For example, I do opengl games. So I just this to let my engine know to rotate the game graphics. But I'm not rotating my view. I don't think many people were using this for device rotation notifications.

    So I decided to use device notifications instead. In my view controller:

    - (void) movingToBackground: (NSNotification *) notification {
        [[UIDevice currentDevice] endGeneratingDeviceOrientationNotifications];
    }
    
    - (void) movingToForeground: (NSNotification *) notification {
        [[UIDevice currentDevice] beginGeneratingDeviceOrientationNotifications];
    }
    

    And to receive the messages:

       [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(viewChanging:) name:UIDeviceOrientationDidChangeNotification object:nil];
    

    And the method:

    - (void)viewChanging:(NSNotification*)notification
    {
       NSLog(@"View is changing");
       previousOrientation = orientation;
       orientation = [[UIDevice currentDevice] orientation];
    
       if (previousOrientation==orientation && forceReorientation==NO) {
           return;
       }
       ... do stuff ...
    

    And then back in my view controller I turned all auto rotation stuff to NO.

    0 讨论(0)
提交回复
热议问题