iOS 横竖屏

iOS6之前的版本中,通常使用 shouldAutorotateToInterfaceOrientation 来单独控制某个UIViewController的方向,需要哪个viewController支持旋转,只需要重写shouldAutorotateToInterfaceOrientation方法。

但是iOS 6里屏幕旋转改变了很多,之前的 shouldAutorotateToInterfaceOrientation 被列为 DEPRECATED 方法,查看UIViewController.h文件也可以看到:

[cpp] view plaincopy

1.   // Applications should use supportedInterfaceOrientations and/or shouldAutorotate..  

2.   (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation NS_DEPRECATED_IOS(2_0, 6_0);  

程序使用了如下2个方法来代替:

[cpp] view plaincopy

1.   (BOOL)shouldAutorotate;  

2.   (NSUInteger)supportedInterfaceOrientations;  

除了重写这个2个方法,IOS6里面要旋转还有一些需要注意的地方,下面会细述。另外还有一个硬性条件,需要在Info.plist文件里面添加程序支持的所有方向,可以通过以下2种方式添加

1.

[转载]IOS6旋转屏、锁定横屏的实现(兼容IOS5)

2.

[转载]IOS6旋转屏、锁定横屏的实现(兼容IOS5)

另外要兼容IOS6之前的系统,要保留原来的 shouldAutorotateToInterfaceOrientation 方法,还有那些 willRotateToInterfaceOrientation 等方法。

     Demoview1时可以自由旋转的视图,从view1跳转到的view2视图是锁定为横屏的,在IOS6中如果是有正常的NavigationController push出来的view是被默认为竖屏的,为避免此限制,用了presentModalViewController方法,将view2锁定横屏的方式呈现出来。View2的导航栏是自定义的一个很像导航栏的view,替代系统的导航栏来执行导航栏的功能!

4.具体实现方式

1创建一个OrientationNavigationController类继承UINavigationConroller,重写两个方法

#pragma mark-支持自动转屏

-(NSUInteger)supportedInterfaceOrientations

{

    //返回顶层视图支持的旋转方向

    return self.topViewController.supportedInterfaceOrientations;

}

- (BOOL)shouldAutorotate

{

    //返回顶层视图的设置

    return self.topViewController.shouldAutorotate;

}

 

(2)AppDelegate中,用此类创建一个导航,并设置成window的根视图

_OrientationNav= [[OrientationNavigationController alloc] initWithRootViewController:self.OrientationView1];

 

self.window.rootViewController=self.OrientationNav;

3)在各自的viewConroller中再重写四个方法,其中最后一个是为了兼容IOS6以下的版本了保留的

#pragma mark-旋屏的方法设置

-(NSUInteger)supportedInterfaceOrientations

{

    [self setMyLayout];

    return UIInterfaceOrientationMaskAllButUpsideDown;

}

- (BOOL)shouldAutorotate

{

    return YES;

}

- (void)willAnimateRotationToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation duration:(NSTimeInterval)durationNS_AVAILABLE_IOS(3_0)

{

    [self setMyLayout];//检测到旋转时就会被调用

}

//为了兼容IOS6以前的版本而保留的方法

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation

{

    //return (toInterfaceOrientation == UIInterfaceOrientationPortraitUpsideDown);

    return YES;//即在IOS6.0以下版本,支持所用方向的旋屏

}

 

(4)然后写一个排版的函数,注意需要在viewwillappear、 supportedInterfaceOrientations 、willAnimateRotationToInterfaceOrientation三处都要重排一下[转载]IOS6旋转屏、锁定横屏的实现(兼容IOS5)

5)由view1跳转到view2中用 presentModalViewControllerf方式

Demo下载地址:IOS6旋转屏、锁屏


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值