我正在使用MGSplitViewController,并使用shouldAutorotateToInterfaceOrientation
来控制主视图控制器在旋转时的大小。
这在iOS 5上都正常工作,但在iOS 6上(模拟器和iPad均是如此),shouldAutorotateToInterfaceOrientation
从未被调用过。
这是我应该期望在iOS 6最终发布时修复的错误,还是有什么我不知道的改变?
我正在使用MGSplitViewController,并使用shouldAutorotateToInterfaceOrientation
来控制主视图控制器在旋转时的大小。
这在iOS 5上都正常工作,但在iOS 6上(模拟器和iPad均是如此),shouldAutorotateToInterfaceOrientation
从未被调用过。
这是我应该期望在iOS 6最终发布时修复的错误,还是有什么我不知道的改变?
事实证明,只有根视图处理这些调用。在我的情况下,这是一个普通的UINavigationController。我不得不将其更改为一个子类文件,在其中添加了这些方法。
在我的情况下,我只想让根视图竖屏显示,其他视图可以横竖屏切换。
Appdelegate.h
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
self.window = [[[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]] autorelease];
// Override point for customization after application launch.
S2MBookAppRootViewController *masterViewController = [[[S2MBookAppRootViewController alloc] initWithNibName:pref.rootNibName bundle:nil] autorelease];
self.navigationController = [[[S2MBookAppNavController alloc] initWithRootViewController:masterViewController] autorelease];
self.window.rootViewController = self.navigationController;
[self.window makeKeyAndVisible];
[[UIApplication sharedApplication] setStatusBarStyle:UIStatusBarStyleBlackOpaque animated:YES];
return YES;
}
同时,S2MBookAppNavController(UINavigationController的子类)
- (BOOL)shouldAutorotate {
return YES;
}
- (NSUInteger)supportedInterfaceOrientations
{
if([self.viewControllers count] == 1) return UIInterfaceOrientationMaskPortrait;
return UIInterfaceOrientationMaskPortrait | UIInterfaceOrientationMaskLandscape;
}
更新: 当你想要强制改变方向(在导航控制器上推入一个新视图时)尝试以下方法。
将你的UINavigationController设为自己的代理:
@interface S2MBookAppNavController : UINavigationController <UINavigationControllerDelegate>
@end
在.m文件中
- (void)viewDidLoad
{
[super viewDidLoad];
self.delegate = self;
// Do any additional setup after loading the view.
}
- (void)navigationController:(UINavigationController *)navigationController didShowViewController:(UIViewController *)viewController animated:(BOOL)animated {
if ([UIViewController respondsToSelector:@selector(attemptRotationToDeviceOrientation)]) {
//present/dismiss viewcontroller in order to activate rotating.
UIViewController *mVC = [[[UIViewController alloc] init] autorelease];
[self presentModalViewController:mVC animated:NO];
[self dismissModalViewControllerAnimated:NO];
}
}
是的,问题只在于window.rootViewController方法调用返回方向掩码。 因此,在设置为window.rootViewController的viewController中必须实现supportedInterfaceOrientations方法。但在大多数情况下,该对象不是自定义类的对象,例如UINavigationController。一种可能的解决方案是子类化UINavigationController。但是Apple表示:“此类不适合子类化”,因此我宁愿使用另一个UIViewController来处理方向,并将UINavigationController添加为子视图。
MyRootViewController * myRoot = [MyRootViewController new];
self.window.rootViewController = myRoot;
[myRoot addChildViewController:navigationController];
[myRoot.view addSubview:navigationController.view];
在MyRootViewController中实现方法:
- (BOOL)shouldAutorotate {
return YES;
}
- (NSUInteger)supportedInterfaceOrientations
{
// return your mask here e.g.:
return UIInterfaceOrientationMaskPortrait;
}
Apple已经在iOS6中弃用了shouldautorate方法,请改用这些方法。请参阅您的Xcode文档。
- (BOOL)shouldAutorotate NS_AVAILABLE_IOS(6_0);
- (NSUInteger)supportedInterfaceOrientations NS_AVAILABLE_IOS(6_0);
// Returns interface orientation masks.
- (UIInterfaceOrientation)preferredInterfaceOrientationForPresentation NS_AVAILABLE_IOS(6_0);
我有一系列的视图控制器在一个UINavigationController中,其中一个必须是横屏模式。我通过子类化UINavigationController并添加以下代码来解决问题:
- (NSUInteger)supportedInterfaceOrientations{
return [[self.viewControllers lastObject] supportedInterfaceOrientations];
}
- (NSUInteger)supportedInterfaceOrientations{
if(UIDeviceOrientationIsPortrait([[UIDevice currentDevice] orientation]))
return UIInterfaceOrientationMaskPortrait;
else
return UIInterfaceOrientationMaskLandscape;
}
-(BOOL)shouldAutorotate
{
UIDeviceOrientation orientation = [UIDevice currentDevice].orientation;
if (orientation == UIDeviceOrientationUnknown) {
return YES;
}
return [self shouldAutorotateToInterfaceOrientation:self.interfaceOrientation];
}
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
至
- (void)viewWillLayoutSubviews
可以考虑直接替换这个部分,但我还没有在旧版 iOS 上进行测试。
如果您想限制支持的方向,应该在 UIApplicationDelegate
层面上进行设置。
-(NSUInteger)application:(UIApplication *)application supportedInterfaceOrientationsForWindow:(UIWindow *)window
supportedInterfaceOrientationsForWindow:
方法返回的值与顶部全屏控制器的supportedInterfaceOrientations方法返回的值相交,来确定是否支持方向。”但在实验中,我发现系统忽略了我的视图控制器的支持。-(NSUInteger)supportedInterfaceOrientations
即使方法被调用,也要返回值。
这对我有效:
- (BOOL)shouldAutorotate {
return YES;
}
- (NSUInteger)supportedInterfaceOrientations {
NSUInteger orientations = UIInterfaceOrientationMaskPortrait;
if ([self isKindOfClass:[YourViewController class]]) { // Your view class
orientations |= UIInterfaceOrientationMaskPortraitUpsideDown;
}
return orientations;
}
方向:
orientations |= UIInterfaceOrientationMaskLandscapeLeft;
orientations |= UIInterfaceOrientationMaskLandscapeRight;