iOS 6迫使设备朝向横向

我给了一个带有10个视图控制器的应用程序。我使用导航控制器加载/卸载它们。


除了一个以外,其他所有设备都处于纵向模式。假设第7个VC处于横向状态。我需要在加载时以横向显示它。


请提出一种在IOS 6中强制将方向从纵向更改为横向的方法(在IOS 5中也同样适用)。


这是我在 IOS 6 之前所做的事情:


- (void)viewWillAppear:(BOOL)animated {

    [super viewWillAppear:animated];

    UIViewController *c = [[[UIViewController alloc]init] autorelease];

    [self presentModalViewController:c animated:NO];

    [self dismissModalViewControllerAnimated:NO];

}

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation{

    return (interfaceOrientation == UIInterfaceOrientationPortrait);

}

呈现和消除模态VC迫使该应用检查其方向,因此shouldAutorotateToInterfaceOrientation被人们称为。


我在IOS 6中尝试过的方法:


- (BOOL)shouldAutorotate{

    return YES;

}

-(NSUInteger)supportedInterfaceOrientations{

    return UIInterfaceOrientationMaskLandscape;

}

- (UIInterfaceOrientation)preferredInterfaceOrientationForPresentation{

    return UIInterfaceOrientationLandscapeLeft;

}

加载时,控制器保持纵向状态。旋转设备后,方向更改就可以了。但是我需要使控制器自动旋转以在负载时横向显示,因此用户将不得不旋转设备才能正确查看数据。


另一个问题:将设备旋转回纵向后,方向变为纵向,尽管我supportedInterfaceOrientations仅在中指定了UIInterfaceOrientationMaskLandscape。为什么会发生?


此外,无上述3种方法越来越被调用。


一些(有用的)数据:


在我的plist文件中,我指定了3个方向-除了上下颠倒外,其他所有方向。

该项目是从Xcode 4.3 IOS 5开始的。包括xibs在内的所有类都是在Xcode 4.5 IOS 6之前创建的,现在我使用的是最新版本。

在plist文件中,状态栏设置为可见。

在xib文件中(我要在横向文件中),状态栏为“无”,方向设置为横向。

任何帮助表示赞赏。谢谢。


iPhone Objective-C 方向 ios6


侃侃无极
浏览 567回答 3
3回答

大话西游666

这应该可以工作,类似于iOS 6之前的版本,但带有UINavigationController:UIViewController *portraitViewController = [[UIViewController alloc] init];UINavigationController* nc = [[UINavigationController alloc] initWithRootViewController:portraitViewController];[self.navigationController presentModalViewController:nc animated:NO];[self.navigationController dismissModalViewControllerAnimated:NO];我先打这个,再推下一个UIViewController。UIViewController即使当前UIViewController处于横向模式,它也会强制将下一个按下的按钮显示为纵向模式(也适用于纵向到横向)。适用于iOS 4 + 5 + 6。

倚天杖

我认为最好的解决方案是坚持使用Apple官方文档。因此,据此我使用以下方法,并且在iOS 5和6上一切正常。在我的VC中,我重写了以下方法:- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation{    // Return YES for supported orientations    return UIInterfaceOrientationIsPortrait(interfaceOrientation);}适用于iOS 6的方法,第一种方法返回支持的方向遮罩(如其名称所示)-(NSUInteger)supportedInterfaceOrientations{    return UIInterfaceOrientationMaskPortrait;}第二个告诉您的VC,当要显示VC时,这是首选的界面方向。- (UIInterfaceOrientation)preferredInterfaceOrientationForPresentation{    return UIInterfaceOrientationPortrait;}只需将Portrait更改为所需的方向即可;)此解决方案运行流畅,我不喜欢围绕此简单解决方案创建宏和其他内容的想法。希望能有所帮助...
打开App,查看更多内容
随时随地看视频慕课网APP