在 UITabBarController 和 UINavigationController 中自动旋转视图 [英] Autorotate view inside UITabBarController and UINavigationController

查看:16
本文介绍了在 UITabBarController 和 UINavigationController 中自动旋转视图的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

My main view controller is a UITabBarController, with 4 UINavigationControllers as the tab bar items. Each navigation bar is a couple of different views that get pushed onto it's stack.

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

I have a single view that I want autorotated, but I can't get willAnimateFirstHalfOfRotationToInterfaceOrientation to be called within that view controller.

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

I've tried subclassing my UITabBarController and UINaviationControllers and adding an override for shouldAutorotateToInterfaceOrientation like so:

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

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

Even after that I can't get the view to rotate. I figured maybe the top-most UIViewController (tab and navigation included) has to be able to rotate. Or even every view up the chain. I've tried overriding shouldAutorotateToInterfaceOrientation for every controller but still can't get the view to rotate.

有人完成了这个或有什么建议吗?

Anyone accomplished this or have any suggestions?

提前致谢!

推荐答案

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

Besides subclassing your tabBarController as you did overriding houldAutorotateToInterfaceOrientation, you must do the following:

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

In the viewDidLoad method of the controller in which you want to add a view to be rotated:

self.view.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight;

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

Add this delegate method in the controller in which you want to add a view to be rotated:

 - (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];
}

}

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

You may also want to add the following method:

- (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 和 navigationController

这篇关于在 UITabBarController 和 UINavigationController 中自动旋转视图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆