ios - 如何强制 View 在 iOS 中进入横向?

标签 ios objective-c

我在 SO 上看到了很多可能的相关问题,但似乎没有一个是重复的:

how to force view to rotate to landscape in viewdidload?

force landscape ios 7

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/

相关文章:

ios - 当我使用hidesBarOnSwipe函数时尝试更改菜单高度

ios - sqlite3_step 在将数据插入 sqlite 时返回错误代码 5

ios - 如何使用用户数据预填充电子邮件

ios - 自从升级到 iOS10 后,带有 NSRunLoop 的信号量无法正常工作

ios - 使用图像属性保存CGImage

iphone - 键盘关闭后 UITableView 无法滚动到底部

ios -\[MediaRemote\] 错误操作需要注册客户端回调。请求播放队列

iphone - 使用CLLocationDistance计算最外层坐标?

ios - 从单元格创建事件 (IBAction)

ios - 重构iOS项目以不使用 Storyboard