如何在Tab Bar + NavigationControllers应用程序中启用横向方向?

时间:2011-10-27 14:17:16

标签: iphone objective-c ios uiviewcontroller uitabbarcontroller

我是以编程方式创建基于标签栏的应用。在应用程序:didFinishLaunchingWithOptions (应用程序委托)方法中,我提出了:

self.tabBarController = [[[UITabBarController alloc] init] autorelease];

SearchViewController *search_vc = [[[SearchViewController alloc] init] autorelease];
SearchBookmarksViewController *search_bookmarks_vc = [[[SearchBookmarksViewController alloc] init] autorelease];
ItemsBookmarksViewController *items_bookmarks_vc = [[[ItemsBookmarksViewController alloc] init] autorelease];
AboutUsViewController *aboutus_vc = [[[AboutUsViewController alloc] init] autorelease];

UINavigationController *search_nav = [[[UINavigationController alloc] initWithRootViewController:search_vc] autorelease];
UINavigationController *search_bookmarks_nav = [[[UINavigationController alloc] initWithRootViewController:search_bookmarks_vc] autorelease];
UINavigationController *items_bookmarks_nav = [[[UINavigationController alloc] initWithRootViewController:items_bookmarks_vc] autorelease];
UINavigationController *aboutus_nav = [[[UINavigationController alloc] initWithRootViewController:aboutus_vc] autorelease];

NSArray vc_stack = [NSArray arrayWithObjects:search_nav,search_bookmarks_nav,items_bookmarks_nav,aboutus_vc,nil];

tabBarController.viewControllers = vc_stack;

[self.window addSubview:tabBarController.view];

SearchViewController基本上是一个搜索表单,在UItableView中显示带有行的结果,选中时会显示有关所选行的详细信息。用户还可以按下“查看照片”按钮,用UIImageView推动视图控制器显示一些照片。

现在,在处理照片视图时,我必须处理横向显示,这在标签栏应用程序中似乎无法轻松实现。你能帮忙做些什么来让这个照片视图控制器处理横向方向?

我被告知要使 shouldAutorotateToInterfaceOrientation 方法在每个视图控制器中返回YES,从而使任何视图允许横向方向或子类TabBarController,这最后一个解决方案可能导致我的应用被拒绝苹果。所以我对做什么感到有点迷惑......

提前帮助你!

的Stephane

3 个答案:

答案 0 :(得分:3)

你必须做的很简单: -

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

为您的每个ViewController。

答案 1 :(得分:2)

我已经使用了你提到的两个选项。

  1. make shouldAutorotateToInterfaceOrientation方法在每个实例化/子类化视图控制器中返回YES:为此,您需要确保每个视图控制器都有其shouldAutorotateToInterfaceOrientation方法返回YES。一个选项是CMD + Shift + F在整个项目中搜索“:UIViewController”,这将显示从UIViewController继承的所有头文件的列表,因此在每个与此对应的实现文件中将shouldAutorotateToInterfaceOrientation设置为YES头。 (这是因为有时您可能已经包含了第三方代码,其中包含一些继承自UIViewController的类,并且您省略将shouldAutorotateToInterfaceOrientation设置为YES)

  2. 子类TabBarController:Apple文档说“这个类不用于子类化。”因此最好避免使用此选项,我已经使用过它并且有效,但是你会总是有可能被Apple拒绝。 (也许不是第一次,但在升级时)

  3. 无论如何,我认为Apple不鼓励tabbar应用程序以横向模式显示,因为tab bar占用了屏幕上的大量空间。因此,最好考虑应用程序的体系结构,看看是否真的需要tabbarcontroller,或者在iPhone上创建自己的tabbarcontroller控件,如Twitter应用程序。这样,您可以在lanscape中减小标签栏的高度。

答案 2 :(得分:1)

除了在每个子视图控制器中启用旋转(如果您在ios5中进行开发,默认情况下启用),如果您想接管整个屏幕,请执行以下操作:

UIInterfaceOrientation toOrientation = self.interfaceOrientation;

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


if(toOrientation == UIInterfaceOrientationLandscapeLeft || toOrientation == UIInterfaceOrientationLandscapeRight) {
    transView.frame = CGRectMake(0, 0, 568, 320);
    self.portraitView.hidden = YES;
    self.landscapeView.hidden = NO;
    tabBar.hidden = TRUE;
    self.wantsFullScreenLayout = YES;
}
else
{
    //Designing to accomodate the largest possible screen on an iPhone. The structs and springs should be setup so that the view will remain consistent from portrait to landscape and back to portrait.
    transView.frame = CGRectMake(0, 0, 320, 568);
    tabBar.hidden = FALSE;
    self.portraitView.hidden = NO;
    self.landscapeView.hidden = YES;
    self.wantsFullScreenLayout = NO;
}
    }`

更新:

https://stackoverflow.com/a/13523577/1807026