I am using X-Code 14 with iOS 16.1. I am trying my app to support portrait and upside down orientation for SE2 iPhone devices. My app has a button on which it should rotate from portrait to UPsidedown and vice versa. Till iOS 15, I will using UIDevice method to change orientation but in iOS 16 it got deprecated.
In iOS 16 my code is working fine for UIInterfaceOrientationMaskPortrait|UIInterfaceOrientationMaskLandscapeLeft|UIInterfaceOrientationMaskLandscapeRight. But it is not working for UIInterfaceOrientationMaskPortraitUpsideDown.
The issue I noticed, in case of up-side-down orientation, the supportedInterfaceOrientationsForWindow in app delegate is not getting called but in case it being called by iOS.
Appdelegate .h @property (nonatomic, assign, getter=isRotateScreen) BOOL rotateScreen;
Appdelegate .m
- (void)setRotateScreen:(BOOL)rotateScreen {
_rotateScreen = rotateScreen; }
- (UIInterfaceOrientationMask)application:(UIApplication *)application supportedInterfaceOrientationsForWindow:(UIWindow *)window {
return self.isRotateScreen ? UIInterfaceOrientationMaskPortraitUpsideDown : UIInterfaceOrientationMaskPortrait; }
ViewController.h @property (weak, nonatomic) id appdelegate;
- (void)p_switchOrientationWithRotateScreen:(BOOL)isLaunchScreen;
-(IBAction) buttonClicked;
ViewController.m
- (void)viewDidLoad {
[super viewDidLoad]; self.appdelegate = (AppDelegate *)[UIApplication sharedApplication].delegate; [self.appdelegate setRotateScreen:false]; }
-(IBAction) buttonClicked{ [self p_switchOrientationWithRotateScreen:[self.appdelegate isRotateScreen] == true?false:true]; }
- (void)p_switchOrientationWithRotateScreen:(BOOL)isRotateScreen {
[self.appdelegate setRotateScreen:isRotateScreen]; if (@available(iOS 16.0, *)){ //NSArray *array = [[[UIApplication sharedApplication] connectedScenes] allObjects]; //UIWindowScene *scene = [array firstObject]; UIWindowScene *scene = [[self.view window] windowScene]; __weak typeof(self) weakSelf = self; dispatch_async(dispatch_get_main_queue(), ^{ // use weakSelf here [weakSelf setNeedsUpdateOfSupportedInterfaceOrientations]; UIInterfaceOrientationMask orientation = isRotateScreen ? UIInterfaceOrientationMaskPortraitUpsideDown : UIInterfaceOrientationMaskPortrait; UIWindowSceneGeometryPreferencesIOS *geometryPreferencesIOS = [[UIWindowSceneGeometryPreferencesIOS alloc] initWithInterfaceOrientations:orientation]; [scene requestGeometryUpdateWithPreferences:geometryPreferencesIOS errorHandler:^(NSError * _Nonnull error) { NSLog(@"Interface %@ with error %@", isRotateScreen ? @"UPSIDE" : @"NORMAL", error); }]; }); } else { [self p_swichToNewOrientation:isRotateScreen ? UIInterfaceOrientationPortraitUpsideDown : UIInterfaceOrientationPortrait]; } }
- (void)p_swichToNewOrientation:(UIInterfaceOrientation)interfaceOrientation {
NSNumber *orientationTarget = [NSNumber numberWithInteger:interfaceOrientation]; [[UIDevice currentDevice] setValue:orientationTarget forKey:@"orientation"]; }