Custom UIWindows do not rotate correctly in iOS 8

前端 未结 4 640
情歌与酒
情歌与酒 2021-02-05 12:46

Get your application into landscape mode and execute the following code:

UIWindow *toastWindow = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];         


        
4条回答
  •  佛祖请我去吃肉
    2021-02-05 12:57

    Your 'fix' isn't great for another reason, in that it doesn't actually rotate the window so that text and other subviews appear with the appropriate orientation. In other words, if you ever wanted to enhance the window with other subviews, they would be oriented incorrectly.

    ...

    In iOS8, you need to set the rootViewController of your window, and that rootViewController needs to return the appropriate values from 'shouldAutoRotate' and 'supportedInterfaceOrientations'. There is some more about this at: https://devforums.apple.com/message/1050398#1050398

    If you don't have a rootViewController for your window, you are effectively telling the framework that the window should never autoRotate. In iOS7 this didn't make a difference, since the framework wasn't doing that work for you anyway. In iOS8, the framework is handling the rotations, and it thinks it is doing what you requested (by having a nil rootViewController) when it restricts the bounds of your window.

    Try this:

    @interface MyRootViewController : UIViewController
    @end
    
    @implementation MyRootViewController
    
    - (UIInterfaceOrientationMask)supportedInterfaceOrientations
    {
        return UIInterfaceOrientationMaskAll;
    }
    
    - (BOOL)shouldAutorotate
    {
        return YES;
    }
    
    @end
    

    Now, add that rootViewController to your window after it is instantiated:

    UIWindow *toastWindow = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
    toastWindow.rootViewController = [[MyRootViewController alloc]init];
    

提交回复
热议问题