ios rotation 横屏竖屏的问题

以下问题只针对ios6之后的横竖屏设置,具体代码如下:

- (BOOL)shouldAutorotate {

return NO;//暂时只支持默认的竖屏

}

- (UIInterfaceOrientationMask)supportedInterfaceOrientations {

returnUIInterfaceOrientationMaskLandscape;

}

- (UIInterfaceOrientation)preferredInterfaceOrientationForPresentation {

returnUIInterfaceOrientationLandscapeLeft;

}

在具体项目中要实现的逻辑是:通过push进到webview,同时让webview一直处于横屏状态。问题出现在设置上述代码后该webview无法横屏,查阅资料发现该页面是从导航push过来导致的,将跳转方式改为present,效果实现了。

但是为什么push过来的视图无法横屏,原因如下:

* 在iOS 4 and 5,都是由具体的view controller来决定对应的view的orientation设置。而在iOS 6,则是由top-most  controller来决定view的orientation设置。

举个例子:你的app的rootViewController是navigation controller "nav", 在”nav"里的stack依次是:main view -> sub view > sub sub view,而main view里有一个button会present modal view "modal view".

那么for ios 4 and 5,在ipad里,如果你要上述view都仅支持横屏orientation,你需要在上面的main view, sub view, sub sub view, model view里都添加

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {

return(interfaceOrientation == UIInterfaceOrientationLandscapeLeft || interfaceOrientation==UIInterfaceOrientationLandscapeRight);

}

而对于iOS6, 由于是由top-most controller来设置orientation,因此你在main view, sub view, sub sub view里添加下面的代码是没有任何效果的,而应该是在nav controller里添加下列代码。而modal view则不是在nav container里,因此你也需要在modal view里也添加下列代码。

-(NSUInteger)supportedInterfaceOrientations{

returnUIInterfaceOrientationMaskLandscape;

}

- (BOOL)shouldAutorotate

{

returnYES;

注意:

*你需要自定义一个UINavigationController的子类for "nav controller",这样才可以添加上述代码。

* 和navigation controller类似,tab controller里的各个view的orientation设置应该放在tab controller里

for ios6 的top-most controller决定orientation设置,导致这样一个问题:在 top-most controller里的views无法拥有不相同的orientation设置。例如:for iphone, 在nav controller里,你有main view, sub view and sub sub view,前2个都只能打竖,而sub sub view是用来播放video,可以打横打竖。那么在ios 4 and 5里可以通过在main view and sub view的shouldAutorotateToInterfaceOrientation里设置只能打竖,而在sub sub view的shouldAutorotateToInterfaceOrientation设置打竖打横即可。而在ios 6里则无法实现这种效果,因为在main view, sub view and sub sub view的orientation设置是无效的,只能够在nav controller里设置。那么你可能想着用下列代码在nav controller里控制哪个view打竖,哪个view打横

-(NSUInteger)supportedInterfaceOrientations{

if([[self topViewController] isKindOfClass:[SubSubViewclass]])

returnUIInterfaceOrientationMaskAllButUpsideDown;

else

returnUIInterfaceOrientationMaskPortrait;

}

这样可以使得在main view and sub view里无法打横,而sub sub view横竖都行。但问题来了,如果在sub sub view时打横,然后back to sub view,那么sub view是打横显示的!

目前想到的解决方法只能是把sub sub view脱离nav controller,以modal view方式来显示。这样就可以在modal view里设置打横打竖,而在nav controller里设置只打竖。

* in iOS 6, 当view controller present时,不会call willRotateToInterfaceOrientation:duration:, willAnimateRotationToInterfaceOrientation:duration:, and didRotateFromInterfaceOrientation: methods,只有在发生rotate的时候才会call

完美 ,问题解决

参考博客

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • iOS屏幕旋转学习笔记iOS开发中使用屏幕旋转功能的相关方法 1、基本知识点解读 了解屏幕旋转首先需要区分两种 o...
    Laughingg阅读 14,625评论 13 39
  • 以下是复制别人的文章过来的,目的是保留下这个方法,便于今后查阅使用,该文章不会用于任何商业用途,该文章的出处:ht...
    鸿蒙一笑万物开阅读 12,739评论 0 3
  • /* UIViewController is a generic controller base class th...
    DanDanC阅读 5,890评论 0 2
  • 网友恶搞的段子: 为你洗了头 护了肤 造了型 擦了cpb的隔离 纪梵希的散粉 喷了迪奥限量的香水 用了YSL明彩笔...
    你沐爺阅读 3,424评论 0 1
  • 姓名:张金友 公司:深圳慧友冠源科技有限公司 272期努力1组 【日精进打卡第24天】 【知~学习】 《六项精进】...
    7ba11b8e59f0阅读 1,228评论 0 0