iOS6.0从横向viewController以纵向方向推送新的viewController

时间:2013-02-15 14:14:37

标签: objective-c uiinterfaceorientation autorotate ios6

我的应用程序是基于导航的,主要以纵向方式运行,但是一个视图控制器同时支持纵向和横向。现在的问题是,当我从横向视图控制器推出新的视图控制器时,新的视图控制器也以横向模式推送,虽然我想要它在纵向模式。

此外,当我从横向控制器弹出视图控制器时,poped视图控制器将以纵向模式显示。

我不知道我的代码有什么问题。

以下是我的代码段和用于这些方向支持的信息。

在info.plist文件中,我保持对所有方向的支持,但是画像上下颠倒。

我还添加了导航控制器类别的类别,如下所示。

@implementation UINavigationController(Rotation_IOS6)
-(BOOL)shouldAutorotate
{
    return [[self.viewControllers lastObject] shouldAutorotate];
}

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

- (UIInterfaceOrientation)preferredInterfaceOrientationForPresentation
{
    return [[self.viewControllers lastObject] preferredInterfaceOrientationForPresentation];
}
@end

我还创建了一个UIViewController子类,它充当所有类的超类。以下是超类的定位方法。

@implementation ParentViewController

- (BOOL)shouldAutorotate{
    return NO;
}

- (NSUInteger)supportedInterfaceOrientations{
    return UIInterfaceOrientationMaskPortrait;
}

-(BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation{
    return (toInterfaceOrientation == UIInterfaceOrientationPortrait);
}

- (UIInterfaceOrientation)preferredInterfaceOrientationForPresentation{
    return UIInterfaceOrientationPortrait;
}
@end

支持横向的方向控制器如下所示。

@implementation LandscapeController
#pragma mark -
#pragma mark Orientation Methods
- (BOOL)shouldAutorotate{
    return YES;
}

- (NSUInteger)supportedInterfaceOrientations{
    return UIInterfaceOrientationMaskPortrait|UIInterfaceOrientationMaskLandscape;
}

-(BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation{
    return (toInterfaceOrientation != UIInterfaceOrientationPortraitUpsideDown);
}
@end

提前致谢。

2 个答案:

答案 0 :(得分:0)

这是推送view.take一个对象开关的方法

在switch.m

+ (void)loadController:(UIViewController*)VControllerToLoad andRelease:(UIViewController*)VControllerToRelease
{

VControllerToLoad.navigationController.navigationBar.frame=CGRectMake(0, 0, 568, 44);
CGRect statusBarFrame = [[UIApplication sharedApplication] statusBarFrame];
CGRect windowFrame = [[UIScreen mainScreen] bounds];
CGRect firstViewFrame = CGRectMake(statusBarFrame.origin.x, statusBarFrame.size.height, windowFrame.size.width, windowFrame.size.height - statusBarFrame.size.height);
VControllerToLoad.view.frame = firstViewFrame;

//check version and go

if (IOS_OLDER_THAN_6)
{
    [((AppDelegate*)[UIApplication sharedApplication].delegate).window addSubview:VControllerToLoad.view];
}
else
{
    [((AppDelegate*)[UIApplication sharedApplication].delegate).window setRootViewController:VControllerToLoad];

}
  [VControllerToRelease.view removeFromSuperview];
}

您可以使用上述方法

从横向模式推送到纵向视图
 PortraitViewController *portraitVC=[[PortraitViewController alloc]initWithNibName:@"PortraitViewController" bundle:nil];

  [Switch loadController:portraitVC andRelease:self];

在landscape view controller.m

    - (void)viewDidLoad
{
    [super viewDidLoad];
    self.navigationController.title=@"Landscape";
    appDelegate = (AppDelegate *)[[UIApplication sharedApplication] delegate];
    appDelegate.navigationController.navigationBarHidden = NO;
    if (IOS_OLDER_THAN_6)
        [[UIApplication sharedApplication] setStatusBarOrientation:UIDeviceOrientationLandscapeLeft animated:NO];
    // Do any additional setup after loading the view from its nib.
}

#ifdef IOS_NEWER_OR_EQUAL_TO_6

-(BOOL)shouldAutorotate  
{
    return YES; 
 }

- (NSUInteger)supportedInterfaceOrientations    
 {
    return UIInterfaceOrientationMaskLandscapeRight | UIInterfaceOrientationMaskLandscapeLeft; 
 }

#endif

答案 1 :(得分:0)

我遇到过类似的问题。在我的情况下,这个非常一个视图控制器必须始终以横向显示,并且在返回的路上必须将其他视图控制器设置回纵向。

我在FAQ中找到了以下解决方案: iPhone Landscape FAQ and Solutions

这个想法的关键点是设备只能通过一种方式强制进入某个方向: 1.设置状态栏的方向。 2.以模态方式呈现任何支持目标方向的视图控制器(!)。

嗯,你不想以模态呈现任何东西?别担心。没有什么可以阻止你... 3.一旦它出现,立即解雇那个视图控制器。当你这样做时,它甚至不会被用户看到。 但一旦完成,您的设备仍然处于这个方向。 4.如果您使用故事板,请按下要显示的视图控制器,甚至可以将其转换为视图控制器。

对我来说,这似乎是唯一可行的解​​决方案。除了FAQ中的答案之外,其他所有内容也必须正确设置。完整项目支持的设备方向必须基本上全部。每个视图控制器支持的方向必须是每个视图控制器支持的方向,shouldAutoRotate应返回YES。 如果涉及标签栏控制器,则会变得更加困难。如果您的应用基于标签栏,请与我联系。