ios 指定某个页面是横屏还是竖屏

来源:互联网 发布:淘宝客qq群现在好做吗 编辑:程序博客网 时间:2024/06/05 00:46

       最近项目中有这种需求,在进入某个界面时可能是横屏也有可能是竖屏,但是项目中大部分界面都是采用竖屏这种情况下就要对这个界面进行单独处理,下面是我的解决方法。

网上搜到的都是这样子的

-(BOOL)shouldAutorotate{    returnYES;}- (NSUInteger)supportedInterfaceOrientations{    returnUIInterfaceOrientationMaskPortrait;}

shouldAutorotate 意思是是否支持自动旋转 supportedInterfaceOrientations意思是旋转的方向。但是我要处理的这个界面进行这样的操作发现这两个方法根本不起作用,困扰之余通过查找资料终于明白了到底怎么回事。

看看官方文档


通过描述我们可以发现这两个方法需要设置在根视图中,意思是说确实这个页面是否旋转旋转的方向都是要根据根视图的这两个方法来判断,真的很操蛋。好了既然知道了这点下面是我的处理办法。
我在我的 
BaseNavigationController 处理如下:

-(BOOL)shouldAutorotate{   returnself.topViewController.shouldAutorotate;}- (NSUInteger)supportedInterfaceOrientations{   returnself.topViewController.supportedInterfaceOrientations;}


这段代码的作用就是 对是否旋转和旋转方向都从当前的控制器去取。
在我的 
BaseViewController 处理如下 :

-(BOOL)shouldAutorotate{   returnYES;}- (NSUInteger)supportedInterfaceOrientations{   returnUIInterfaceOrientationMaskPortrait;}


这样做的目的就是如果一些界面没有使用这两个方法那默认就是竖屏。
通过这样做就可以实现可以单独在某个控制实现单独的横屏和竖屏如:

-(BOOL)shouldAutorotate{    returnYES;}- (NSUInteger)supportedInterfaceOrientations{    return UIInterfaceOrientationMaskLandscapeRight;}

这里还多介绍一个方法,比如我们需要点击某个按钮的时候要控制器进行旋转,这个时候就需要进行强制旋转下面两个方法就可以实现:

[[UIDevicecurrentDevice]setValue:[NSNumbernumberWithInteger:UIDeviceOrientationLandscapeLeft]forKey:@"orientation"];[[selfclass]attemptRotationToDeviceOrientation];






1 0
原创粉丝点击