iOS部分页面横竖屏解决方案

来源:互联网 发布:mac迅雷下到46没速度 编辑:程序博客网 时间:2024/06/06 04:49

系统支持横屏顺序
默认读取plist里面设置的方向(优先级最高)等同于Xcode Geneal设置里面勾选
application window设置的级别次之

然后是UINavigationcontroller/UITabbarController

级别最低的是viewcontroller

(注意Xcode Geneal设置里面没有勾选的方向viewcontroller强制旋转到该方向会crash)

旋转屏幕相关的几个重要方法:

//是否能旋转屏幕

- (BOOL)shouldAutorotate {

    return YES;

}

//支持旋转的方向

- (UIInterfaceOrientationMask)supportedInterfaceOrientations

{

    return UIInterfaceOrientationMaskAllButUpsideDown;

}

//强制旋转屏幕,私有方法 direction:UIInterfaceOrientation

NSNumber * value = [NSNumbernumberWithInteger:direction];

[[UIDevicecurrentDevice]setValue:valueforKey:@"orientation"];


几个旋转屏幕的小技巧:

1.项目中大多数页面都不支持旋转在rootViewController中shouldAutorotate方法中返回NO即可,对于支持旋转的我们使用presentViewController方法推出页面。

2.从支持旋转的页面A,push到不支持旋转的页面B 这里实现一个UINavigationController类重写一下方法:

//是否能旋转屏幕

- (BOOL)shouldAutorotate {

    returnself.topViewController.shouldAutorotate;

}

//支持旋转的方向

- (UIInterfaceOrientationMask)supportedInterfaceOrientations

{

    returnself.topViewController.supportedInterfaceOrientations;

}

在B页面shouldAutorotate方法中返回NO,推出B页面之前强制旋转为竖屏即可。

3.在设备已经旋转为横屏的状态下,从竖屏A,present到支持横屏的页面B,发现推出的直接是横屏,为了交互的一致性,希望推出的还是竖屏,可以在viewDidLoad,和viewDidAppear方法中先后设置self.needAutorotate为NO,和YES

- (UIInterfaceOrientationMask)supportedInterfaceOrientations

{

    if (self.needAutorotate ==YES) {

        returnUIInterfaceOrientationMaskAllButUpsideDown;

    }

    returnUIInterfaceOrientationMaskPortrait;

}

4.设备强制旋转后键盘弹出的方向不对,一定是supportedInterfaceOrientations返回的参数不对,调试下就ok了

5.对于一会可以自由旋转一会不能旋转的需求也是对supportedInterfaceOrientations返回参数做文章。


原创粉丝点击