我在 SO 上看到了很多可能的相关问题,但似乎没有一个是重复的:
how to force view to rotate to landscape in viewdidload?
IOS - How to force the view to landscape
Force landscape for one view controller ios
iOS - Force Landscape Orientation
How to make app fully working correctly for autorotation in iOS 6?
我的应用是基于选项卡的应用。
目前进入某些 View 时,我可以将手机旋转到横屏,让我的 View 进入横屏。
主要代码:
// In app delegate
- (UIInterfaceOrientationMask)application:(UIApplication *)application supportedInterfaceOrientationsForWindow:(UIWindow *)window {
if (_allowRotation == YES) {
return UIInterfaceOrientationMaskLandscape;
}
else {
return UIInterfaceOrientationMaskPortrait;
}
}
在我想要横向的 View 中,我将 allowRotation
设置为 YES。
但是我想强制 View 进入景观。
换句话说,当进入某些 View 时,它会自动进入横向,无需旋转手机。甚至用户将屏幕锁定为纵向。 如何实现这一点?
我使用的是 iOS 10。
最佳答案
您必须覆盖要将其移动到横向的特定 View 的 onDidLoad 或 onDidAppear,例如我将其添加到 viewWillAppear 和 Disappear:
- (void)viewWillAppear:(BOOL)animated
{
[super viewWillAppear:YES];
self.navigationController.navigationBarHidden = NO;
NSNumber *value = [NSNumber numberWithInt:UIInterfaceOrientationLandscapeRight];
[[UIDevice currentDevice] setValue:value forKey:@"orientation"];
}
-(void)viewWillDisappear:(BOOL)animated {
NSNumber *value = [NSNumber numberWithInt:UIInterfaceOrientationPortrait];
[[UIDevice currentDevice] setValue:value forKey:@"orientation"];
[super viewWillDisappear:animated];
}
所以我们的 View 显示应用程序移动到横向,当移动消失时它又回到纵向。对于 appDelegate,您必须添加以下内容:
- (UIInterfaceOrientationMask)application:(UIApplication *)application supportedInterfaceOrientationsForWindow:(UIWindow *)window
{
UINavigationController *navigationController = (UINavigationController *)self.window.rootViewController;
if ([navigationController.viewControllers.lastObject isKindOfClass:[OpencvViewController class]])
{
return UIInterfaceOrientationMaskLandscape;
}
else
return UIInterfaceOrientationMaskPortrait;
}
关于ios - 如何强制 View 在 iOS 中进入横向?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42489852/