前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >iOS6中横屏的处理方法 原

iOS6中横屏的处理方法 原

作者头像
珲少
发布2018-08-15 11:49:10
1K0
发布2018-08-15 11:49:10
举报
文章被收录于专栏:一“技”之长一“技”之长

IOS6以后,若想在项目中支持横屏,我们首先需要在plist文件中添加支持横屏的设置,否则有些代码设置将会失效。

有来那个方式设置:

1、在pilist的Supported interface orientations 字段中添加

2、在Xcode的设置中勾选

现在我们来看决定屏幕方向的几个函数:

在IOS6之前,我们只需通过一个函数

- (BOOL) shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation {     return (toInterfaceOrientation == UIInterfaceOrientationLandscapeRight); }

就可以支持指定控制器的旋转。通过新的文档,我们可以看到:

代码语言:javascript
复制
// Applications should use supportedInterfaceOrientations and/or shouldAutorotate..
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation
     NS_DEPRECATED_IOS(2_0, 6_0);
//这个方法在6.0之后被标记为过时的

我们通过下面两个方法来代替:

//是否允许屏幕旋转

-(BOOL)shouldAutorotate{     return YES; } //支持的方向 - (NSUInteger)supportedInterfaceOrientations {     return UIInterfaceOrientationMaskLandscapeRight; } 这是个枚举

代码语言:javascript
复制
typedef NS_OPTIONS(NSUInteger, UIInterfaceOrientationMask) {
    UIInterfaceOrientationMaskPortrait = (1 << UIInterfaceOrientationPortrait),
    UIInterfaceOrientationMaskLandscapeLeft = (1 << UIInterfaceOrientationLandscapeLeft),
    UIInterfaceOrientationMaskLandscapeRight = (1 << UIInterfaceOrientationLandscapeRight),
    UIInterfaceOrientationMaskPortraitUpsideDown=(1 << UIInterfaceOrientationPortraitUpsideDown),
    UIInterfaceOrientationMaskLandscape = (UIInterfaceOrientationMaskLandscapeLeft 
                                        | UIInterfaceOrientationMaskLandscapeRight),
    UIInterfaceOrientationMaskAll = (UIInterfaceOrientationMaskPortrait 
                                    | UIInterfaceOrientationMaskLandscapeLeft
                                    | UIInterfaceOrientationMaskLandscapeRight 
                                    | UIInterfaceOrientationMaskPortraitUpsideDown),
    UIInterfaceOrientationMaskAllButUpsideDown = (UIInterfaceOrientationMaskPortrait 
                                    | UIInterfaceOrientationMaskLandscapeLeft 
                                    | UIInterfaceOrientationMaskLandscapeRight),
};

通过这两个函数,如果我们需要某个控制器强制方向,我们可以设置支持单一的方向,即可达到目的。

注意:

如果你们项目中的RootViewController是导航,你会发现,你在Push出来的视图中添加刚才的代码并没有起作用,原因是导航,并没有进行设置,我们创建一个文件,继承于NavigationController。在里面重写刚才的方法,这么做后,屏幕确实横了过来,并且这个导航push的所有子界面都将横屏,这也不是我们想要的效果。我们想自由的控制每个push出来的界面的屏幕方向,可以在导航里这么做:

代码语言:javascript
复制
-(BOOL)shouldAutorotate{
    return [self.topViewController shouldAutorotate];
}
//支持的方向
- (NSUInteger)supportedInterfaceOrientations {
    return [self.topViewController supportedInterfaceOrientations];;
}

我们还需要做一些处理,经过我的测试,导航必须在pop后才会重新调用这些函数,所以我的方法是这样做:弹出一个中间控制器后再POP回来

代码语言:javascript
复制
@implementation ViewController2

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    [self.navigationController pushViewController:[[ViewController3 alloc]init] animated:YES];
}
代码语言:javascript
复制
@implementation ViewController3

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    [self.navigationController popViewControllerAnimated:YES];
}

这样做,我们就可以自由的控制每个视图控制器的方向了。

同理,如果根视图控制器是tabBar,则我们需要在tabBar中做操作。

如果我们大多是的视图控制器都是一个方向的,只有偶尔的几个会不同,这时候,我们其实可以采取presentationController的方式,然后直接在弹出的控制器中写那两个方法即可。这是最简单的途径了。

专注技术,热爱生活,交流技术,也做朋友。 ——珲少 QQ群:203317592

本文参与 腾讯云自媒体分享计划,分享自作者个人站点/博客。
原始发表:2015/04/07 ,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档