iOS UIInterfaceOrientation

iOS UIInterfaceOrientation

我的 iOS 应用同时支持横向和纵向。初始屏幕、登录视图和主页视图都是纵向的。

现在,如果我将设备旋转到横向并启动应用程序,所有三个视图都是并保持纵向,这是需要的。但是如果我以纵向启动应用程序并且在启动画面期间我旋转设备以横向登录屏幕显示为横向然后自动移动到纵向。主屏相同(自动登录,当用户已经登录并重新打开应用程序时,它直接进入主屏幕)。

我用过这个

[[UIDevice currentDevice] setValue:[NSNumber numberWithInt:UIInterfaceOrientationPortrait] forKey:@"orientation"];

在 viewDidLoad、viewDidAppear、viewWillAppear 和 viewWillDisappear 中。我也用过

-(NSUInteger)supportedInterfaceOrientations{
return UIInterfaceOrientationMaskPortrait;
}

所以,我想要实现的是,即使我在启动画面期间旋转设备,也能保持纵向视图。

谢谢。

尝试将其放入您的 viewDidLoad 调用中:

// Change to portrait if in landscape mode
if (UIDeviceOrientationIsLandscape([[UIDevice currentDevice] orientation])) {
    NSNumber *value = [NSNumber numberWithInt:UIInterfaceOrientationPortrait];
    [[UIDevice currentDevice] setValue:value forKey:@"orientation"];
}

并且记得在 supportedInterfaceOrientations

中调用 super
-(NSUInteger)supportedInterfaceOrientations{
   [super supportedInterfaceOrientations];
   return UIInterfaceOrientationMaskPortrait;
}

希望对您有所帮助。

您应该覆盖 viewController 的 shouldAutorotate 方法,并且 return 在那些您不想旋转的控制器中 NO

编辑:尝试注释掉 shouldAutorotate 方法并实施

- (NSUInteger)supportedInterfaceOrientations {
    return UIInterfaceOrientationMaskPortrait;
}

导致该问题的应用程序流程存在问题。 Ryan 和 Soberman 提供的解决方案很有帮助。这些代码行应该可以正常工作 supportedInterfaceOrientations NSNumber *value = [NSNumber numberWithInt:UIInterfaceOrientationPortrait]; [[UIDevice currentDevice] setValue:value forKey:@"orientation"];