我如何强制方向为特定的viewController仅为iOS 15.4.1?



我有一个可以在iPad和iPhone上运行的应用程序。我需要硬编码的方向为一个特定的viewController的x原因。在ios15.4.1的Objective-c中还能做到吗?我知道过去有人问过这个问题,但我找不到任何答案,他们在以前的ios版本中被问到。

我试过了:

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

和这个:

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

还有更多。

谢谢。

我找到了一个修复!一个奇怪的修复,但仍然。我不认为这是一个好方法,也不是那么强大,但它现在工作…

问题是,你不能设置设备的方向forKey:@"orientation"如果设备已经在该位置(该值不会改变)。所以你的视图不会旋转(我的猜测是,当值被改变时,有一个触发器)。

要解决这个问题,你要么必须物理地旋转设备(用户可能不知道他必须这样做),要么你可以强制forKey:@"orientation"要改变,这样你可以再次改变它,然后设备将旋转。

在这里:

- (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"];

我发现它必须是在viewDidLoad函数或它不会工作。

简单,丑陋,但是高效。

最新更新