0

我的主视图控制器是 a UITabBarController,其中 4UINavigationControllers作为标签栏项目。每个导航栏都是几个不同的视图,它们被推送到它的堆栈上。

我有一个想要自动旋转的视图,但我无法willAnimateFirstHalfOfRotationToInterfaceOrientation在该视图控制器中被调用。

我已经尝试将我的UITabBarControllerand子类化UINaviationControllers并添加一个覆盖,shouldAutorotateToInterfaceOrientation如下所示:

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
    return YES;
}

即使在那之后,我也无法旋转视图。我想也许最上面的UIViewController(包括标签和导航)必须能够旋转。甚至是链条上的每个视图。我已经尝试覆盖shouldAutorotateToInterfaceOrientation每个控制器,但仍然无法旋转视图。

有人完成了这项工作或有任何建议吗?

提前致谢!

4

3 回答 3

1

您可以使用 SubClassed UITabBarController(或使用添加)来询问所选的 navigationController 对visibleViewController旋转请求的响应:

@implementation RotatingTabBarController
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
        if([self.selectedViewController isKindOfClass:[UINavigationController class]]){
           return [[(UINavigationController*)self.selectedViewController visibleViewController] shouldAutorotateToInterfaceOrientation:interfaceOrientation];
        } else {
           return [self.selectedViewController shouldAutorotateToInterfaceOrientation:interfaceOrientation];
        }
}
@end
于 2009-11-17T01:07:22.740 回答
1

除了像覆盖 houldAutorotateToInterfaceOrientation 那样子类化 tabBarController 之外,还必须执行以下操作:

在要在其中添加要旋转的视图的控制器的 viewDidLoad 方法中:

self.view.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight;

在要在其中添加要旋转的视图的控制器中添加此委托方法:

 - (void)didRotateFromInterfaceOrientation:(UIInterfaceOrientation)fromInterfaceOrientation
{
    //NSLog(@"didRotateFromInterfaceOrientation");


    if((fromInterfaceOrientation == UIInterfaceOrientationPortrait) ||
       (fromInterfaceOrientation == UIInterfaceOrientationPortraitUpsideDown))
    {    

        YourAppDelegate *mainDelegate = (YourAppDelegate *)[[UIApplication sharedApplication] delegate];
    [mainDelegate.tabBarController.view addSubview: viewToBeRotated];

    [viewToBeRotated setHidden:NO];

    return;

}

if(fromInterfaceOrientation == UIInterfaceOrientationLandscapeLeft || fromInterfaceOrientation == UIInterfaceOrientationLandscapeRight)
{
    [viewToBeRotated removeFromSuperview];
    [self.view setHidden:NO];
}

}

您可能还想添加以下方法:

- (void)willAnimateFirstHalfOfRotationToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation 
                                                duration:(NSTimeInterval)duration {

if (toInterfaceOrientation == UIInterfaceOrientationPortrait)
{
    //self.view = self.portrait;
    YourAppDelegate *mainDelegate = (YourAppDelegate *)[[UIApplication sharedApplication] delegate];
    [viewToBeRotated removeFromSuperview];
    mainDelegate.tabBarController.view.transform = CGAffineTransformIdentity;
    mainDelegate.tabBarController.view.transform = CGAffineTransformMakeRotation(degreesToRadian(0));
    mainDelegate.tabBarController.view.bounds = CGRectMake(0.0, 0.0, 300.0, 480.0);

}
else if (toInterfaceOrientation == UIInterfaceOrientationLandscapeLeft)
{
    YourAppDelegate *mainDelegate = (YourAppDelegate *)[[UIApplication sharedApplication] delegate];
    [mainDelegate.tabBarController.view addSubview: viewToBeRotated];
    mainDelegate.tabBarController.view.transform = CGAffineTransformIdentity;
    mainDelegate.tabBarController.view.transform = CGAffineTransformMakeRotation(degreesToRadian(-90));
    mainDelegate.tabBarController.view.bounds = CGRectMake(0.0, 0.0, 460.0, 320.0);
}
else if (toInterfaceOrientation == UIInterfaceOrientationPortraitUpsideDown)
{


//self.view = self.portrait;
        YourAppDelegate *mainDelegate = (YourAppDelegate *)[[UIApplication sharedApplication] delegate];
        [viewToBeRotated removeFromSuperview];
        mainDelegate.tabBarController.view.transform = CGAffineTransformIdentity;
        mainDelegate.tabBarController.view.transform = CGAffineTransformMakeRotation(degreesToRadian(180));
        mainDelegate.tabBarController.view.bounds = CGRectMake(0.0, 0.0, 300.0, 480.0);
    }
    else if (toInterfaceOrientation == UIInterfaceOrientationLandscapeRight)
    {
        YourAppDelegate *mainDelegate = (YourAppDelegate *)[[UIApplication sharedApplication] delegate];
        [mainDelegate.tabBarController.view addSubview: viewToBeRotated];
        mainDelegate.tabBarController.view.transform = CGAffineTransformIdentity;
        mainDelegate.tabBarController.view.transform = CGAffineTransformMakeRotation(degreesToRadian(90));
        mainDelegate.tabBarController.view.bounds = CGRectMake(0.0, 0.0, 460.0, 320.0);
    }
}

您可能还想看看

旋转 iPhone,并实例化一个新的 UIViewController

横向模式下的 TabBarController 和 navigationControllers

于 2009-06-15T23:08:07.033 回答
0

继承 UITabBarController 并覆盖此方法有效:

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
    return YES;
}

我认为我真正的问题是 XCode 没有编译更新的代码。我做了干净的构建,触摸并重新启动了 XCode 和模拟器,最终进行了更改。

于 2009-06-15T23:46:57.270 回答