记录于2013/8/5
 
在切换横竖屏的时候调用到的一些委托方法:
#pragma mark - UIApplicationDelegate
//写在Appdelegate中,在具体的某一视图控制器没有重写supportedInterfaceOrientations或者shouldAutorotateToInterfaceOrientation的情况下指定该视图的支持方向;如果该方法没有实现,则应用程序使用Info.plist文件中默认设置的值
- (NSUInteger)application:(UIApplication*)application supportedInterfaceOrientationsForWindow:(UIWindow*)window{
   //返回UIInterfaceOrientationMask类型数据 ,可以找到具体某一个视图控制器加以控制
   return UIInterfaceOrientationMaskPortrait;
}
 
#pragma mark - UIViewController
//>=ios6.0
// 是否自动旋转方向 设置为NO时其实就不会旋转了
- (BOOL)shouldAutorotateNS_AVAILABLE_IOS(6_0);
// 返回该视图控制器支持的方向,返回UIInterfaceOrientationMaskPortrait类型数据
- (NSUInteger)supportedInterfaceOrientations NS_AVAILABLE_IOS(6_0);
// 在使用presentViewController时调用该方法,只能返回一个数值,其他设置对该界面没有影响,不过在该界面前两个方法就不要调用了
- (UIInterfaceOrientation)preferredInterfaceOrientationForPresentation{
    returnUIInterfaceOrientationLandscapeRight;
}
 
//<=ios5
// 在ios6之后已遗弃,不过适配ios5之前的版本时需要加上
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation NS_DEPRECATED_IOS(2_0, 6_0);
 
 
如果支持横竖屏则需要重绘视图:
写一个排版的函数,注意需要在viewwillappear、 supportedInterfaceOrientations 、willAnimateRotationToInterfaceOrientation三处都要重排一下
 
 

ios6中需要指定某一个界面为横竖屏,其他界面与之不同,现在试验得出以下方法暂时可能,还没找到会出问题
对于iOS6, 由于是由top-most controller来设置orientation       [self topViewController]就是指向当前页面的视图控制器
一、当主控制器为UINavigationController时,继承重写UINavigationController,在其.m文件中编写如下代码,表示某个具体类支持的方向
 
有两种方法:
1、只在UINavigationController继承的子类中设置方向,其他页面都不需要设置 (这里的方法只有在页面初始化和旋转的时候才会调用到)
-(NSUInteger)supportedInterfaceOrientations{
    if([[self topViewController] isKindOfClass:[ThirdViewController class]])
        return UIInterfaceOrientationMaskAllButUpsideDown;
    return UIInterfaceOrientationMaskPortrait;
}
- (BOOL)shouldAutorotate{
    return YES;
}
 
2、下面代码写在UINavigationController继承的子类中, 同时还要在对应的页面重写这些代码(默认是支持除倒置意外的所有方向)
-(NSUInteger)supportedInterfaceOrientations{
    //返回顶层视图支持的旋转方向
    return self.topViewController.supportedInterfaceOrientations;
}
- (BOOL)shouldAutorotate{
    //返回顶层视图的设置
    return self.topViewController.shouldAutorotate;
}
 
3、其实有时候也可以将以上两个结合起来使用,指定某些页面调用第二种方法,并在那个页面中重写。 其他的设置返回某一中类型。
 
注意:需要在.plist文件中配置所有支持的方向,且在AppDelegate和其他界面不要编写控制转向的代码。 AppDelegate中写了有出错,其他界面还没发现,不过暂时觉得最好还是不要编写。
 
 
但是上面的方法有个不好的地方是:这样可以使得在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里设置只打竖。
现在找到的方法是在sub view 页面上页加上
-(NSUInteger)supportedInterfaceOrientations{(当前视图支持的方向)
    returnUIInterfaceOrientationMaskPortrait; 
}
 
同时presentViewController方法是不受NavigationController控制的
或者采取以下方法来加载页面,就可以在该页面设置转向代码。 已脱离UINavgationController
- (void)presentViewController:(UIViewController *)viewControllerToPresent animated: (BOOL)flag completion:(void (^)(void))completion
 
 
 
 

 
 
UIViewController的shouldAutorotateToInterfaceOrientation方法被deprecated。在ios6里,是使用supportedInterfaceOrientations and shouldAutorotate 2个方法来代替shouldAutorotateToInterfaceOrientation。注意:为了向后兼容iOS 4 and 5,还是需要在你的app里保留shouldAutorotateToInterfaceOrientation。
 
for ios 4 and 5, 如果没有重写shouldAutorotateToInterfaceOrientation,那么对于iphone来讲,by default是只支持portrait,不能旋转。
 
for ios 6, 如果没有重写shouldAutorotate and supportedInterfaceOrientations,by default, iphone则是"可以旋转,支持非upside down的方向",而ipad是"可以选择,支持所有方向"
 
example 1: for ios 4 and 5, iphone device, 若要"可以旋转,支持非upside down的方向",则可以在view controller里
[cpp]
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation { 
return (interfaceOrientation != UIDeviceOrientationPortraitUpsideDown); 
 
example 2: for ios 6, iphone device, 若要“不能旋转,只支持portait",则可以在view controller里
[cpp] 
- (BOOL)shouldAutorotate 
return NO; 
 
example 3: for ios 6, ipad device, 若要“可以旋转,只支持landscape",则可以在view controller里
[cpp] 
-(NSUInteger)supportedInterfaceOrientations{ 
爀攀琀甀爀渀 UIInterfaceOrientationMaskLandscape; 
 
- (BOOL)shouldAutorotate 
return YES; 
 
 
* 在iOS 4 and 5,都是由具体的view controller来决定对应的view的orientation设置。而在iOS 6,则是由top-most决定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里都添加
 
[cpp] 
- (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里也添加下列代码。
[cpp] 
-(NSUInteger)supportedInterfaceOrientations{ 
return UIInterfaceOrientationMaskLandscape; 
 
- (BOOL)shouldAutorotate 
return  YES; 
 
注意:
*你需要自定义一个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打横
 
[cpp] 
-(NSUInteger)supportedInterfaceOrientations{ 
if([[self topViewController] isKindOfClass:[SubSubView class]]) 
return UIInterfaceOrientationMaskAllButUpsideDown; 
else 
return UIInterfaceOrientationMaskPortrait; 
 
是的,这样可以使得在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里设置只打竖。
 
 
* 说了那么多,其实如果你的app的所有view的orientation的设置是统一的,那么你可以简单的在plist file里设置即可,不用添加上面的代码。而如果你添加了上面的代码,就会覆盖plist里orientation的设置。
 
 
* in iOS 6, 当view controller present时,不会call willRotateToInterfaceOrientation:duration:, willAnimateRotationToInterfaceOrientation:duration:, and didRotateFromInterfaceOrientation: methods,只有在发生rotate的时候才会call
 
 
 
有的是自己的root controller,试一下
 
    //[self.window addSubview:myRootViewConroller.view]; 替换成下面的情况
    // Begin 6.0
    if ([[[UIDevice currentDevice] systemVersion] floatValue] >= 6.0) {
        
        self.window.rootViewController = myRootViewConroller;
    }
    else
    {
        [self.window addSubview:myRootViewConroller.view];
    }
    // End 6.0
 

ios6和ios5横竖屏切换的更多相关文章

  1. Android APP 简单高效的禁用横竖屏切换

    默认情况下,Android APP的界面会随着手机方向的改变而改变,当手机处于竖屏状态,APP的界面也处于竖屏状态,而当手机处于横屏状态,APP也会自动切换到横屏状态.一般情况下APP的界面都是为竖屏 ...

  2. Android开发之Activity横竖屏切换生命周期重建问题

    当进行横竖屏切换的时候Activity的生命周期会重建,从而导致Activity崩溃等问题,为了避免这一问题,需要在AndroidManifest.xml文件中设置: <activity and ...

  3. Activity 横竖屏切换

    前言 在开发中常要处理横竖屏切换,怎么处理先看生命周期 申明 Activity 横竖屏切换时需要回调两个函数 ,所以在此将这个两个函数暂时看成是Activity 横竖屏切换的生命周期的一部分,这两个函 ...

  4. Android应用:横竖屏切换总结

    眨眼间,已经到了2016你年春节前,离上一篇博客的时间已经有6个月多,回想起这半年的种种,不得不说,学习和工作实在是太忙了,或许这就是程序员的真实写照吧. 写博客之初,主要的目的还是为了把自己的学习痕 ...

  5. Android横竖屏切换

    ps:虽然现在的app一般都是固定一个屏幕方向,但是还是有必要了解下屏幕切换的方法和注意. 一 固定横竖屏 androidmainfest.xml中设置activoty属性:android:scree ...

  6. Android横竖屏切换及其对应布局加载问题

    第一,横竖屏切换连带横竖屏布局问题: 如果要让软件在横竖屏之间切换,由于横竖屏的高宽会发生转换,有可能会要求不同的布局. 可以通过以下两种方法来切换布局: 1)在res目录下建立layout-land ...

  7. Android横竖屏切换重载问题与小结

    (转自:http://www.cnblogs.com/franksunny/p/3714442.html) (老样子,图片啥的详细文档,可以下载后观看 http://files.cnblogs.com ...

  8. Android 横竖屏切换小结

    (自己体会:每次横竖屏自动切时都会run Activity的onCreate,即相当后重新进入Activity初始化一样:) 转自:http://www.cnblogs.com/franksunny/ ...

  9. 横竖屏切换时,Activity的生命周期

    横竖屏切换时,Activity的生命周期 1.新建一个Activity,并把各个生命周期打印出来 2.运行Activity,得到如下信息 onCreate-->onStart-->onRe ...

随机推荐

  1. DIV内文字两端对齐

    div{ text-align: justify; text-justify:inter-ideograph; }

  2. P3203 [HNOI2010]弹飞绵羊

    LCT裸题,之后填坑打一下 分块做法:每个点存几次出块以及出块的位置,问的时候直接暴力跳就vans了 首先思考最普通的模拟,发现可以O(n)路径压缩,O(1)的查询,但是需要修改就变成了O(n^2)的 ...

  3. 虚拟机14安装kail Linux

    需要准备虚拟机和kail Linux镜像 1. 2.选择镜像安装,并且添加你的kail Linux镜像文件. 3. 4.在这里需要修改虚拟机名称,也可以不修改就用默认,然后在修改kail Linux的 ...

  4. 【模板】最长公共子序列(LCS)。

    看过好多人的博客,感觉要么是太复杂要么就是太不容易理解. 那就亲自动手写一个通俗易懂的. 先定义两个数组,第一个数组为主,用第二个数组来匹配第一个,看能有多少可以对应上的. 所以,其实第一个数组的内容 ...

  5. 1、Altium Designer 入门

    一.新建工程 File-->new-->Project-->newPCB Project 1.添加原理图 在Project面板选中项目,右键Add New to Project--& ...

  6. 在vscode中使用eslint+prettier格式化vue项目代码 (转载)

    ESlint:javascript代码检测工具,可以配置每次保存时格式化js,但每次保存只格式化一点点,你得连续按住Ctrl+S好几次,才格式化好,自行体会~~ vetur:可以格式化html.标准c ...

  7. HTML和XHTML区别

    HTML和XHTML 可扩展超文本标记语言XHTML(eXtensible HyperText Markup Language)是将超文本标记语言HTML(HyperText Markup Langu ...

  8. Java基础14-缓冲区字节流;File类

    作业解析 阐述BufferedReader和BufferedWriter的工作原理, 是否缓冲区读写器的性能恒大于非缓冲区读写器的性能,为什么,请举例说明? 答: BufferedReader对Rea ...

  9. Virtual Machine

    之前说到可以使用Assembly language来实现程序编写,把程序通过一个Assembler就可以得到计算机可以操作的二进制文件. 但是Assembly language依旧不适于编程,但怎么将 ...

  10. python学习第30天

    tcp协议的socket server 并发效果验证客户端的合法性socket模块还有一些其他的方法