Custom UIWindows do not rotate correctly in iOS 8

前端 未结 4 586
情歌与酒
情歌与酒 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];
    
    0 讨论(0)
  • 2021-02-05 13:09

    In iOS 7 and earlier, UIWindow's coordinate system did not rotate. In iOS 8 it does. I am guessing the frame supplied from [[UIScreen mainScreen] bounds] does not account for rotation, which could cause issues like what you're seeing.

    Instead of getting the frame from UIScreen, you could grab the frame from the appDelegate's current key window.

    However, it does not appear you really need functionality supplied by UIWindow. I'd like to echo others' recommendation that you use a UIView for this purpose. UIView is more generic than UIWindow, and should be preferred.

    0 讨论(0)
  • 2021-02-05 13:17

    I believe you have to convert the UICoordinateSpace.

    On iPhone 6 Plus apps can now launch already in landscape orientation, which messed up an application I am working on since it only supports portrait orientation throughout most of the app, except one screen (meaning it needed to support landscape orientations in the plist).

    The code that fixed this was as follows:

    self.window = [[UIWindow alloc] initWithFrame:[self screenBounds]];
    

    and calculating the bounds with the following code:

    - (CGRect)screenBounds
    {
        CGRect bounds = [UIScreen mainScreen].bounds;
        if ([[UIScreen mainScreen] respondsToSelector:@selector(fixedCoordinateSpace)]) {
            id<UICoordinateSpace> currentCoordSpace = [[UIScreen mainScreen] coordinateSpace];
            id<UICoordinateSpace> portraitCoordSpace = [[UIScreen mainScreen] fixedCoordinateSpace];
            bounds = [portraitCoordSpace convertRect:[[UIScreen mainScreen] bounds] fromCoordinateSpace:currentCoordSpace];
        }
        return bounds;
    }
    

    Hopefully this will lead you in the right direction.

    0 讨论(0)
  • 2021-02-05 13:21

    The best thing to do is use views instead of windows:

    Most iOS applications create and use only one window during their lifetime. This window spans the entire main screen [...]. However, if an application supports the use of an external display for video out, it can create an additional window to display content on that external display. All other windows are typically created by the system

    But if you think you've got a valid reason to create more than one window, I suggest you create a subclass of NSWindow that handles sizes automatically.

    Also note that windows use a lot of RAM, especially on 3x retina screens. Having more than one of them is going to reduce the amount of memory the rest of your application can use before receiving low memory warnings and eventually being killed.

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