且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

IOS6中横屏的处理方法

更新时间:2022-02-26 20:25:54

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

有来那个方式设置:

1、在pilist的Supported interface orientations 字段中添加IOS6中横屏的处理方法

2、在Xcode的设置中勾选IOS6中横屏的处理方法

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

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

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

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

1
2
3
4
// 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;
}
这是个枚举

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
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出来的界面的屏幕方向,可以在导航里这么做:

1
2
3
4
5
6
7
-(BOOL)shouldAutorotate{
    return [self.topViewController shouldAutorotate];
}
//支持的方向
- (NSUInteger)supportedInterfaceOrientations {
    return [self.topViewController supportedInterfaceOrientations];;
}

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

1
2
3
4
5
6
7
@implementation ViewController2
 
- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    [self.navigationController pushViewController:[[ViewController3 alloc]init] animated:YES];
}
1
2
3
4
5
6
7
@implementation ViewController3
 
- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    [self.navigationController popViewControllerAnimated:YES];
}

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

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

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