ios6之前的旋屏方法
- (BOOL)shouldAutorotateToInterfaceOrientation: (UIInterfaceOrientation)interfaceOrientation {
//return (interfaceOrientation == UIInterfaceOrientationMaskAllButUpsideDown);
return (interfaceOrientation == UIInterfaceOrientationMaskAll);
}
对ios6不起作用,只支持portait"。
iOS 6的rotation改变了很多。先来看看官方的描述 http://www.bgr.com/2012/08/06/ios-6-beta-4-change-log-now-available/
在ios6中UIViewController的shouldAutorotateToInterfaceOrientation方法被deprecated。替换使用supportedInterfaceOrientations and shouldAutorotate
- (BOOL)shouldAutorotate {
returnNO;
}
- (NSUInteger)supportedInterfaceOrientations {
return UIInterfaceOrientationMaskPortrait;
}
2个方法来代替shouldAutorotateToInterfaceOrientation。注意:为了向后兼容iOS 4 and 5,还是需要在你的app里保留shouldAutorotateToInterfaceOrientation。
在iOS6之前,都是由具体的view controller来决定对应的当前view的orientation设置。而在iOS 6中,则是由top-most controller(最顶层的View)来决定当前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 {
returnUIInterfaceOrientationMaskLandscape;
}
而对于iOS6, 由于是由top-most controller来设置orientation,因此你在main view, sub view, sub sub view里添加下面的代码是没有任何效果的,而应该是在nav controller里添加下列代码。而modal view则不是在nav container里,因此你也需要在modal view里也添加下列代码。
- (BOOL)shouldAutorotate {
returnYES;
}
- (NSUInteger)supportedInterfaceOrientations {
return UIInterfaceOrientationMaskLandscape;
}
)
对于 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:[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里设置只打竖。
在iOS 6中, 当view controller present时,不会call willRotateToInterfaceOrientation:duration:, willAnimateRotationToInterfaceOrientation:duration:, and didRotateFromInterfaceOrientation: methods,只有在发生rotate的时候才会call。
总结:ios6在NavigationController下面实现任意页面旋转控制注意
*你需要自定义一个UINavigationController的子类for "nav controller",这样才可以添加上述代码。
* 和navigation controller类似,tab controller里的各个view的orientation设置应该放在tab controller里
举例说明:
1.
先子类化UINavigationController,增加方法
// MyNavigationViewController.h
// Created by user on 13-2-25.
// Copyright (c) 2013年 jiangshiyong. All rights reserved.
//
#import <UIKit/UIKit.h>
@interface MyNavigationViewController : UINavigationController {
}
@end
#import "MyNavigationViewController.h"
@interfaceMyNavigationViewController ()
@end
@implementation MyNavigationViewController
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil{
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {
// Custom initialization
} return self;
}
- (void)viewDidLoad{
[superviewDidLoad];}
- (void)didReceiveMemoryWarning{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
return (interfaceOrientation == UIInterfaceOrientationMaskAll);
}
- (BOOL)shouldAutorotate {
return self.topViewController.shouldAutorotate;
}
- (NSUInteger)supportedInterfaceOrientations {
return self.topViewController.supportedInterfaceOrientations;
}
@end
2.AppDelegate.m文件中
#define kIphoneVersion [[UIDevice currentDevice] systemVersion]//获取当前系统版本
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
self.viewController = [[[ViewControlleralloc] init] autorelease];//主页面
if ([kIphoneVersionfloatValue]>=4.0) {
/*
ios 4.0SDK之后版本变成self.window.rootViewController = viewController;
之前版本变成[self.window addSubview:[viewController view]];
*/
if ([kIphoneVersion floatValue]>=6.0){
self.navigationController = [[[MyNavigationViewControlleralloc] initWithRootViewController:self.viewController] autorelease];
self.window.rootViewController = self.navigationController;
}else{
UINavigationController *nav = [[[UINavigationControlleralloc] initWithRootViewController:self.viewController]autorelease];
self.window.rootViewController = nav;
}
}else{
UINavigationController *nav = [[[UINavigationControlleralloc] initWithRootViewController:self.likelistViewController]autorelease];
[self.window addSubview:[nav view]];
}
[self.windowmakeKeyAndVisible];
returnYES;
}
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
return (interfaceOrientation == UIInterfaceOrientationPortrait);
}
- (BOOL)shouldAutorotate {
returnNO;
}
- (NSUInteger)supportedInterfaceOrientations {
returnUIInterfaceOrientationMaskPortrait;
}
比如我想在
#import "MyMoviePlayViewController.h"
moviePalyViewController = [[MyMoviePlayViewControlleralloc]initWithContentURL:[NSURLURLWithString:@""]];
[self presentModalViewController:moviePalyViewController animated:YES];
切换到MyMoviePlayViewController里面使用横屏
就在MyMoviePlayViewController里面添加
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
//return (interfaceOrientation ==UIInterfaceOrientationMaskLandscape);
returnUIInterfaceOrientationIsLandscape(interfaceOrientation);
}
- (BOOL)shouldAutorotate {
returnYES;
}
- (NSUInteger)supportedInterfaceOrientations {
returnUIInterfaceOrientationMaskLandscape;
}
参考
http://blog.csdn.net/totogogo/article/details/8002173
http://blog.csdn.net/yiyaaixuexi/article/details/8035014