-
First of all, my program supports Portrait, Left and Right
-
Customize an AViewCOntroller, AViewCOntroller supports orientation Portrait, Left, Right, click on the screen to create a BViewCOntroller object and rotate it to landscape.()
@interface AViewCOntroller ()
@end
@implementation AViewCOntroller
#pragma mark - life method
- (void)viewDidLoad{
[super viewDidLoad];
}
- (UIInterfaceOrientationMask)supportedInterfaceOrientations{
return UIInterfaceOrientationMaskAllButUpsideDown;
}
#pragma mark - event method
- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event{
BViewCOntroller *vc =[[BViewCOntroller alloc] init];
[self presentViewController:vc animated:YES completion:nil];
[self _dealOrientation:UIInterfaceOrientationMaskLandscapeLeft vc:vc];
}
- (void)_dealOrientation:(UIInterfaceOrientationMask)orientation vc:(UIViewController *)vc{
if (@available(iOS 16.0, *)) {
if (vc) {
NSArray *array = [[[UIApplication sharedApplication] connectedScenes] allObjects];
UIWindowScene *ws = (UIWindowScene *)array[0];
UIWindowSceneGeometryPreferencesIOS *geometryPreferences = [[UIWindowSceneGeometryPreferencesIOS alloc] init];
geometryPreferences.interfaceOrientations = orientation;
[ws requestGeometryUpdateWithPreferences:geometryPreferences
errorHandler:^(NSError * _Nonnull error) {
}];
[vc setNeedsUpdateOfSupportedInterfaceOrientations];
}
} else {
[[UIDevice currentDevice] setValue:@(UIDeviceOrientationUnknown) forKey:@"orientation"];
[[UIDevice currentDevice] setValue:@(orientation) forKey:@"orientation"];
}
}
@end
- window.rootViewController = [[AViewCOntroller alloc] init]
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
self.window = [[UIWindow alloc] initWithFrame:[UIScreen mainScreen].bounds];
self.window.rootViewController = [[AViewCOntroller alloc] init];
[self.window makeKeyAndVisible];
return YES;
}
- Customize an BViewCOntroller,BViewCOntrollersupports orientation Portrait, Left, Right。Implemented the preferredInterfaceOrientationForPresentation method。
@interface BViewCOntroller : UIViewController
@end
@implementation BViewCOntroller
- (void)viewDidLoad { [super viewDidLoad]; }
- (UIInterfaceOrientationMask)supportedInterfaceOrientations{
return UIInterfaceOrientationMaskAllButUpsideDown;
}
- (UIInterfaceOrientation)preferredInterfaceOrientationForPresentation {
return UIInterfaceOrientationLandscapeLeft;
}
@end
- When the program is run at this time, the device orientation defaults to vertical screen, so the first time the AViewCOntroller interface is loaded is also vertical screen. At this time, click the screen to create a BViewCOntroller object. Result: Before iOS16, preferredInterfaceOrientationForPresentation took effect, and BViewCOntroller was displayed horizontally for the first time. In IOS16 Bate 6, preferredInterfaceOrientationForPresentation is invalid, and BViewCOntroller will be displayed vertically for the first time.