iOS 屏幕根据感应自动旋转

来源:互联网 发布:常见tcp端口号 编辑:程序博客网 时间:2024/05/17 07:30

1、如果是全局设置

1)、在工程targets直接勾选device orientation


2)、在项目的根视图控制器中

-(BOOL)shouldAutorotate{

    returnYES;

}

- (UIInterfaceOrientationMask)supportedInterfaceOrientations

{

    returnUIInterfaceOrientationMaskAll;

}

2、如果是某些页面支持旋转
1)、在根视图里

-(BOOL)shouldAutorotate{

    //这里修改为根据navigationController或者tabarController的shouldAutoRotate属性设置返回值

    //returnYES;

}

- (UIInterfaceOrientationMask)supportedInterfaceOrientations

{

    returnUIInterfaceOrientationMaskAll;

}

在需要实现旋转的界面中重写

-(BOOL)shouldAutorotate{

    return YES;

}

- (UIInterfaceOrientationMask)supportedInterfaceOrientations

{

    returnUIInterfaceOrientationMaskPortrait;

}

原理应该是:只有根视图中实现的两个方法起决定性作用,虽然子界面中的两个方法也会调用,但他只会通过改变navigationController、tabarController的shoulAutoRotate属性值将返回值传给根视图控制器中的方法,由根视图控制器完成操作
2)、为AppDelegate添加一个属性值_isRotate,在AppDelegate中实现方法

//  每次试图切换的时候都会走的方法,用于控制设备的旋转方向.

-(UIInterfaceOrientationMask)application:(UIApplication *)application supportedInterfaceOrientationsForWindow:(UIWindow *)window {

    if (_isRotate) {

        returnUIInterfaceOrientationMaskAll;

    }else {

        returnUIInterfaceOrientationMaskPortrait;

    }

}

然后在需要支持旋转的界面中添加一个单例模式的属性_myAppDelegate,

-(void)viewWillAppear:(BOOL)animated{

    _myAppDelegate=[[UIApplicationsharedApplication]delegate];

    _myAppDelegate.isRotation=YES;

}

-(void)viewWillDisappear:(BOOL)animated{

    [super viewWillDisappear:animated];

    _myAppDelegate.isRotation=NO;

}


每次发生方向变化就会Appdelegate中的方法会被调用,此时会根据单例的appDelegate的属性_isRotate判断是否发生改变


3、对设备横竖屏问题的修正和补充

1)不管是整个应用支持多方向还是部分界面支持多方向,首先需要让整个应用支持多方向,最好的方法是在info.plist中Supported interface orientations中只支持portraint,然后在appDelegate中用代码实现(我自己遇到在info.plist中支持多方向,应用加载页面会根据设备方向显示方向)

-(UIInterfaceOrientationMask)application:(UIApplication *)application supportedInterfaceOrientationsForWindow:(UIWindow *)window {

        returnUIInterfaceOrientationMaskAll;

}

然后在每个根视图控制器中实现方法

-(BOOL)shouldAutorotate{

    return YES;

}

- (UIInterfaceOrientationMask)supportedInterfaceOrientations

{

    returnUIInterfaceOrientationMaskPortrait;

}

我们一般用tabbarController和navigationController作为根视图控制器管理子控制器的旋转方向,例如一个tabbarController

@interface UITabBarController (RotateTabBarController)

@end

#import "UITabBarController+RotateTabBarController.h"

@implementation UITabBarController (RotateTabBarController)

a)-(BOOL)shouldAutorotate

{

    UINavigationController *vc = (UINavigationController *)self.selectedViewController;

    

    if ([vc isEqual: [self.viewControllers objectAtIndex:1]]) {

        

        return YES;

    }    

    return NO;

}


b)-(NSUInteger)supportedInterfaceOrientations

{

    UINavigationController *vc = (UINavigationController *)self.selectedViewController;

    

    if ([vc isEqual: [self.viewControllers objectAtIndex:1]]) {

        

        return UIInterfaceOrientationMaskAllButUpsideDown;

    }

    

    return UIInterfaceOrientationMaskPortrait;

}



如果是navigationController,由它推出的所有子控制器跟navigationController中设置是一样的(或者也可以像tabbarController那样根据index控制?没试),在子控制器中重写上面两个方法是无效的。

c)/-(UIInterfaceOrientation)preferredInterfaceOrientationForPresentation

//{

//    UINavigationController *vc = (UINavigationController *)self.selectedViewController;

//    

//    if ([vc isEqual: [self.viewControllers objectAtIndex:1]]) {

//        

//        return  UIInterfaceOrientationMaskAllButUpsideDown;

//    }

//    

//    return UIInterfaceOrientationPortrait;

//}



2)每次通过presentViewController模态推出视图控制器的时候,就得重写上面a、b两个方法,这种时候如果推出的视图较多,就可以选择再建立一个navigationController作为根视图控制器像上面那样管理,为了简化工作,我们可以通过重写navigationController的子类在其中新添加上面两个方法或者通过navigationcontroller的category方式新增两个方法,之后每次使用这个navigationController的子类作为根视图控制器。Note:上面a、b两个方法调用顺序是先检查supportedInterfaceOrientations,再判断shouldAutorotate,所以在模态推出新控制器时,不管新控制器是否支持shouldAutorotate,会根据当前设备方向调整当前界面方向,模态推出之前的多方向设置是失效的,如果该新控制器只支持单一方向,可以在supportedInterfaceOrientations中只写单一方向,如果多方向,就需要在模态推出前的视图控制器的根视图控制器中添加上面c方法


preferredInterfaceOrientationForPresentation

此方法也属于UIViewController. 影响当前viewController的初始显示方向. 此方法也仅有在当前viewControllerrootViewController或者是modal模式时才生效.













0 0
原创粉丝点击
热门问题 老师的惩罚 人脸识别 我在镇武司摸鱼那些年 重生之率土为王 我在大康的咸鱼生活 盘龙之生命进化 天生仙种 凡人之先天五行 春回大明朝 姑娘不必设防,我是瞎子 刚拿到驾照没车怎么办 驾驶证过期了6年怎么办 驾驶证过期7个月怎么办 驾驶换证过期了怎么办 和喜欢的人聊天冷场怎么办 老公和小三有了小孩怎么办 战网账号忘记了怎么办 战网邮箱忘了怎么办 捷信分期逾期了怎么办 欠捷信7万还不了怎么办 苹果6s阴阳屏怎么办 碰到碰瓷的人怎么办 如果遇到碰瓷的怎么办 对交通事故责任认定书不服怎么办 违停15天没处理怎么办 衣服反光条掉了怎么办 脸过敏起小疙瘩怎么办 过敏怎么办怎么好得快 眉毛在眉骨下面怎么办 踩到地雷怎么办知乎 在边境踩到地雷怎么办 插在花泥里的花怎么办 瓶插绣球花蔫了怎么办 水养绣球花蔫了怎么办 鲜切绣球花蔫了怎么办 崩坏2仓库满了怎么办 dnf88级没任务了怎么办 0号柴油冻住了怎么办 不小心喝了生水怎么办 不小心吃了蟑螂怎么办 以租代购还不起怎么办 孩子被教官打了怎么办 三岁宝宝叛逆期怎么办 三岁宝宝很叛逆怎么办 孩子不听话怎么办有什么方法呢 打了三岁的宝宝怎么办 2岁半宝宝不听话怎么办 心里素质不好容易紧张怎么办 孩子二年级成绩差怎么办 遇到素质低的人怎么办 孩子上课注意力不集中怎么办