简体   繁体   中英

How can I force orientation for a certain viewController only for iOS 15.4.1?

I have an application that can run on both iPad and iPhone. I Need to hardcode the orientation for a specific viewController for x reason. Can we still do that on ios 15.4.1 in Objective-c? I Know this question have been asked in the past but i could not find any answer and they were asked in previous ios version.

I tried this :

 -(BOOL)shouldAutorotate { return YES; } -(UIInterfaceOrientationMask)supportedInterfaceOrientations { return UIInterfaceOrientationMaskPortrait; } -(UIInterfaceOrientation)preferredInterfaceOrientationForPresentation { return UIInterfaceOrientationPortrait; }

And this :

 NSNumber *value = [NSNumber numberWithInt:UIInterfaceOrientationMaskPortrait]; [[UIDevice currentDevice] setValue:value forKey:@"orientation"]; [UIViewController attemptRotationToDeviceOrientation];

And much more. nothing worked so far.

Thanks.

I found a fix!! A weird fix, but still. I don't think this is the good way and not that robust but its working for now..

The problem is, you cant set the device orientation forKey:@"orientation" if the device is already in that position (the value won't change). So your view won't rotate (My 2 cent guess is that there is a trigger when the value is changed).

To work around that, you either have to rotate the device physically (the user might not know that's what he have to do) OR you can force the value of forKey:@"orientation" to change so you can change it again and the device will then rotate.

Here it is :

 - (void)viewDidLoad { [super viewDidLoad]; UIDeviceOrientation currentDeviceOrientation = [[UIDevice currentDevice] orientation]; if(currentDeviceOrientation == UIDeviceOrientationPortrait){ NSNumber *value = [NSNumber numberWithInt:UIInterfaceOrientationLandscapeLeft]; [[UIDevice currentDevice] setValue:value forKey:@"orientation"]; } NSNumber *value = [NSNumber numberWithInt:UIInterfaceOrientationPortrait]; [[UIDevice currentDevice] setValue:value forKey:@"orientation"];

And I found out that it HAS to be in the viewDidLoad function or it wont work.

Simple, ugly, but efficient.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM