简体   繁体   English

iOS 6旋转:推动视图控制器

[英]iOS 6 Rotation: Pushing a View Controller

I want to support iOS 6 rotation. 我想支持iOS 6轮换。 Trouble is, I've been looking through a lot of documentation and stack overflow questions but have not found any even slightly in depth solutions. 麻烦的是,我一直在查看大量的文档和堆栈溢出问题,但没有找到任何甚至是稍微深入的解决方案。 I've only seen that I should add these two methods to my view controller classes - however, if I'm not mistaken, they do not operate in the same way as the pre iOS 6 methods: 我只是看到我应该将这两个方法添加到我的视图控制器类中 - 但是,如果我没有弄错,它们的操作方式与iOS 6之前的方法不同:

- (BOOL)shouldAutorotate
{
    return YES;
}

- (NSUInteger)supportedInterfaceOrientations
{
    return UIInterfaceOrientationMaskAll; // use what is appropriate for you.
}

My app currently rotates in pre-iOS6 using the following code. 我的应用程序目前使用以下代码在iOS6之前轮换。 Note that I use the interface orientation parameters to determine whether or not I'm going to push my view Controller. 请注意,我使用界面方向参数来确定是否要按下我的视图控制器。 How do I implement this in the iOS 6 rotation delegates? 如何在iOS 6轮换代理中实现此功能?

-(void)didRotateFromInterfaceOrientation:(UIInterfaceOrientation)fromInterfaceOrientation {
    UIInterfaceOrientation toOrientation = self.interfaceOrientation;

    if ( self.tabBarController.view.subviews.count >= 2 )
    {
        UIView *tabBar = [self.tabBarController.view.subviews objectAtIndex:1];

        if(toOrientation != UIInterfaceOrientationLandscapeLeft && toOrientation != UIInterfaceOrientationLandscapeRight)
        {
            CUSTOM_DEBUG_LOG("\n\nRotated back to Portrait");
            tabBar.hidden = FALSE;
        }
    }
}

- (void) willAnimateRotationToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation duration:(NSTimeInterval)duration
{
    if (interfaceOrientation == UIInterfaceOrientationLandscapeLeft || interfaceOrientation == UIInterfaceOrientationLandscapeRight)
    {
        CUSTOM_DEBUG_LOG("\nView going landscape");
        ScrollViewController *s = [[ScrollViewController alloc] initWithNibName:@"ScrollViewController" bundle:nil];
        [self.navigationController pushViewController:s animated:NO];
        [s release];
        self.tabBarController.tabBar.hidden = YES;
        self.navigationController.navigationBar.hidden = YES;
    }

}

Parent Views now handle rotation in iOS 6. Subclass your nav controllers and add a bool 父视图现在可以处理iOS 6中的旋转。对导航控制器进行子类化并添加一个布尔值

- (BOOL)shouldAutorotate {
    return YES;
}

- (NSUInteger)supportedInterfaceOrientations {
    return UIInterfaceOrientationMaskPortrait;  // your rotation here
}

Checkout this and this SO discussion. 查看 SO讨论。

[EDIT] [编辑]

Yes the methods you mentioned aren't deprecated in iOS 6.0 and they will continue working. 是的,你提到的方法在iOS 6.0中不被弃用,它们将继续工作。 It's just the way Auto Rotation works have been changed. 这就是Auto Rotation工作方式的改变。 So far it was view controllers responsibility to decide whether they rotate or not but now RootViewController will decide whether their children should rotate or not. 到目前为止,视图控制器有责任决定它们是否旋转,但现在RootViewController将决定他们的孩子是否应该旋转。 If you don't have rootviewcontroller setup then you have to add it to window and then put shouldAutoRotate and supportedInterfaceOrientations methods in the rootviewcontroller. 如果你没有rootviewcontroller设置,那么你必须将它添加到窗口,然后在rootviewcontroller中放入shouldAutoRotate和supportedInterfaceOrientations方法。

I may not have implemented the iOS6 rotation code correctly when I first posted the question. 我第一次发布问题时可能没有正确实现iOS6轮换代码。

I incorrectly thought that the willAnimateRotationToInterfaceOrientation function was deprecated in iOS6, leading me to believe that there was a new iOS rotation delegate with an orientation parameter. 我错误地认为在iOS6中不推荐使用willAnimateRotationToInterfaceOrientation函数,这让我相信有一个带有orientation参数的新的iOS旋转委托。 Turns out this is not the case, so my app sort of works. 事实证明并非如此,所以我的应用程序有点工作。

The code I plugged into my app was just this: 我插入我的应用程序的代码就是这样:

- (BOOL)shouldAutorotate
{
    return YES;
}

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

If you are using a UINavigationController, override shouldAutomaticallyForwardRotationMethods = YES property. 如果您使用的是UINavigationController,则覆盖shouldAutomaticallyForwardRotationMethods = YES属性。

Then like Mark S said, also override shouldAutorotate and supportedInterfaceOrientations for the children VCs. 然后像Mark S所说的那样,也为孩子VC重写了shouldAutorotatesupportedInterfaceOrientations

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM