在IOS6中强制横向视图

时间:2013-01-22 16:05:23

标签: ios uiviewcontroller ios6 uinavigationcontroller landscape

我知道有很多线索关于如何在IOS6中强制定位但是它们似乎都不适合我,所以现在我需要一些帮助来解决这个问题。

我有一个基于导航的应用程序,它有许多视图控制器。所有这些都是纵向视图,必须以横向模式加载(不需要用户先打开手机)。

在我的导航控制器的实现中,我添加了shouldAutorotate,supportedInterfaceOrientations和preferredInterfaceOriantationForPresentation。

@implementation UINavigationController (Rotation_IOS6)

- (BOOL)shouldAutorotate
{
    return [self.topViewController shouldAutorotate];
}

- (NSUInteger)supportedInterfaceOrientations
{
    return [self.topViewController supportedInterfaceOrientations];
}

- (UIInterfaceOrientation)preferredInterfaceOrientationForPresentation
{
    return [self.topViewController preferredInterfaceOrientationForPresentation];
}

@end

因此它返回在每个视图控制器中定义的值。

在app委托中,我有supportedInterfaceOrientationsForWindow。

- (NSUInteger)application:(UIApplication *)application supportedInterfaceOrientationsForWindow:(UIWindow *)window{

//Default orientations value
NSUInteger orientations = UIInterfaceOrientationMaskAllButUpsideDown;

//Get orientation from the last view controller
if(self.window.rootViewController){
    UIViewController *presentedViewController = [[(UINavigationController *)self.window.rootViewController viewControllers] lastObject];
    orientations = [presentedViewController supportedInterfaceOrientations];
}

return orientations;
}

在每个视图控制器中,我都有我对该视图的设置,例如:

// Only allow portrait view
-(NSInteger)supportedInterfaceOrientations{    
    return UIInterfaceOrientationMaskPortrait;
}

- (BOOL)shouldAutorotate {
    return YES;
}

我推动下一个视图控制器:

NextViewController *nxtController = [[NextViewController alloc] initWithNibName:@"NextViewController" bundle:nil];
[self.navigationController pushViewController:nxtController animated:YES];

但是当我按下横向视图控制器时,在纵向握住手机时,它也会以纵向模式加载。如果我然后倾斜手机它会触发自动旋转功能并将视图旋转到横向模式,然后它将锁定在该模式中。但是我需要在横向模式下锁定它而不使用手机方向来触发它来检查自动旋转。 有什么想法吗?

2 个答案:

答案 0 :(得分:1)

您可以尝试使用始终返回格局的shouldaAutorotateToInterfaceOrientation强制viewController在横向中显示,例如:

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
    return ((interfaceOrientation == UIInterfaceOrientationLandscapeLeft) || (interfaceOrientation == UIInterfaceOrientationLandscapeRight));
}

答案 1 :(得分:1)

转到项目设置并删除您不想要的模式的选择。