如何在iOS中强制视图进入风景? [英] How to force view to enter landscape in iOS?

查看:63
本文介绍了如何在iOS中强制视图进入风景?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在SO上看到了十几个可能的相关问题,但似乎没有人重复:

I've see a dozen of possible related questions on SO, but no one seems duplicated:

如何强制视图旋转viewdidload?

force landscape ios 7

IOS-如何强制视图变为风景

强制使用一个视图控制器ios的风景

iOS-强制横向景观

如何使应用程序能够完全正常运行以在iOS 6中自动旋转?

我的应用是基于标签的应用.

My app is a tab-based application.

当前,当输入某些视图时,我可以将手机旋转到横向,以使视图进入横向.

Currently when entering certain views, I can rotate my phone to landscape to let my view enter landscape.

主要代码:

// In app delegate
- (UIInterfaceOrientationMask)application:(UIApplication *)application supportedInterfaceOrientationsForWindow:(UIWindow *)window {
    if (_allowRotation == YES)  {
        return UIInterfaceOrientationMaskLandscape;
    }
    else {
        return UIInterfaceOrientationMaskPortrait;
    }
}

在我想成为风景的视图中,将allowRotation设置为是".

In views that I want to be landscape, I set allowRotation to YES.

但是我想强迫视图进入风景.

换句话说,在输入某些视图时,它会自动进入风景,而无需旋转手机.甚至用户也将屏幕锁定为纵向.如何实现?

In other word, when entering certain views, it automatically enters landscape, no need to rotate phone. Even user locks the screen as portrait. How to achieve this?

我正在使用iOS 10.

I'm using iOS 10.

推荐答案

您必须覆盖要移动到横向的特定视图的onDidLoad或onDidAppear,例如,我将其添加到viewWillAppear和Disappear中:

You have to override the onDidLoad or onDidAppear of the specific view that you want to move it to landscape, for example i added it to viewWillAppear and 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];
}

所以我们看到的视图是应用程序移动到横向,而当移动消失时,它又回到纵向.对于appDelegate,您必须添加以下代码:

so we the view appear the application move to landscape and when the move disappear it come again to portrait. and for the appDelegate you have to add this:

- (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中强制视图进入风景?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆