简体   繁体   English

如何仅针对 iOS 15.4.1 强制某个 viewController 的方向?

[英]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.我有一个可以在 iPad 和 iPhone 上运行的应用程序。 I Need to hardcode the orientation for a specific viewController for x reason.由于 x 原因,我需要对特定 viewController 的方向进行硬编码。 Can we still do that on ios 15.4.1 in Objective-c?我们还能在 Objective-c 的 ios 15.4.1 上做到这一点吗? 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.我知道这个问题过去曾被问过,但我找不到任何答案,他们在以前的 ios 版本中被问过。

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).问题是,如果设备已经在那个位置(值不会改变),你不能为 Key:@"orientation" 设置设备方向。 So your view won't rotate (My 2 cent guess is that there is a trigger when the value is changed).所以你的视图不会旋转(我的 2 美分猜测是当值改变时有一个触发器)。

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.要解决这个问题,您要么必须物理旋转设备(用户可能不知道他必须这样做),要么您可以强制更改 forKey:@"orientation" 的值,以便您可以再次更改它和设备然后将旋转。

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.我发现它必须在 viewDidLoad 函数中,否则它将不起作用。

Simple, ugly, but efficient.简单,丑陋,但高效。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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