ios6和ios5横竖屏切换

记录于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
 
posted @ 2019-01-03 17:26  小、  阅读(239)  评论(0编辑  收藏  举报