In the iOS16 system,interfaceOrientation、[UIScreen mainScreen].bounds,keyWindow.bounds exception

In the iOS16 system, when the horizontal and vertical screen rotation occurs, at some point, interfaceOrientation=landspace, [UIScreen mainScreen].bounds, UIWindowScene.screen.bounds.size, keyWindow.bounds.size exception will occur

NSArray *array = [[[UIApplication sharedApplication] connectedScenes] allObjects];

    UIWindowScene *ws = (UIWindowScene *)array[0];

    NSLog(@"interfaceOrientation:%d",ws.interfaceOrientation);

    NSLog(@"screen:%@,screen:%@,keywindow:%@",NSStringFromCGSize(ws.screen.bounds.size),NSStringFromCGSize([UIScreen mainScreen].bounds.size),NSStringFromCGSize(ws.keyWindow.bounds.size));

---------out

2022-08-25 16:32:04.536120+0800 app[2008:61285] interfaceOrientation:3

2022-08-25 16:32:04.536305+0800 app[2008:61285] screen:{375, 812},screen:{375, 812},keywindow:{812, 375}

(lldb) 

The orientation is landscape, but [UIScreen mainScreen].bounds or UIWindowScene.screen.bounds.size is the value in the portrait state, and the value of keyWindow.bounds.size is the value in the landscape state.

What method are you logging these values from?

UIScreen, definitely has different semantics in terms of bounds and orientation than UIWindowScene or UIWindow. So, if you are in the middle of a rotation, looking at UIScreen is the wrong thing to look at here. You should prefer to look at either UIWindow or the UIWindowScene for bounds and layout information. I suspect if you log ws.coordinateSpace.bounds above, it will be a similar interface oriented bounds as the window.

Related, I'd generally recommend not looking at UIScreen for layout information anymore. It still has its uses, but if you have a connected UIWindowScene you should always prefer information source from it instead of UIScreen.

I also have this problem, when I use

SEL selUpdateSupportedMethod = NSSelectorFromString(@"setNeedsUpdateOfSupportedInterfaceOrientations");
    if ([nav respondsToSelector:selUpdateSupportedMethod]) {
        (((void (*)(id, SEL))[nav methodForSelector:selUpdateSupportedMethod])(nav, selUpdateSupportedMethod));
    }
if ([[UIDevice currentDevice] respondsToSelector:@selector(setOrientation:)]) {
            SEL selector = NSSelectorFromString(@"setOrientation:");
            NSInvocation *invocation = [NSInvocation invocationWithMethodSignature:[UIDevice instanceMethodSignatureForSelector:selector]];
            [invocation setSelector:selector];
            [invocation setTarget:[UIDevice currentDevice]];
            NSInteger val = orientation;
            [invocation setArgument:&val atIndex:2];
            [invocation invoke];
}

to rotate the screen to landscape and then use this code to rotate back to portrait. My UIScreen.main.bounds.size is still the size of the landscape screen, and UIDeviceOrientation(rawValue: 1) ---- UIInterfaceOrientation(rawValue: 3).

Xcode 13.2. iOS 16 beta7.

In the iOS16 system,interfaceOrientation、[UIScreen mainScreen].bounds,keyWindow.bounds exception
 
 
Q