自定义TabBar导航问题

时间:2013-03-05 15:11:20

标签: iphone ios uinavigationcontroller uitabbarcontroller

我实现了一个自定义标签栏控制器作为一组按钮,每个按钮都与它自己的View Controller相关。我引导这个链接http://idevrecipes.com/2011/01/04/how-does-the-twitter-iphone-app-implement-a-custom-tab-bar/来实现这种行为。所以代码的相关部分如下:

- (void) selectedItemAtIndex:(NSUInteger)itemIndex
{
// Get the right view controller
NSDictionary* data = [self.tabBarItems objectAtIndex:itemIndex];
UIViewController* viewController = [data objectForKey:@"viewController"];

// Remove the current view controller's view
UIView* currentView = [self.view viewWithTag:SELECTED_VIEW_CONTROLLER_TAG];
[currentView removeFromSuperview];


// Set the view controller's frame to account for the tab bar (+ 48)
viewController.view.frame = CGRectMake(0,48,self.view.bounds.size.width, self.view.bounds.size.height - 48);

// Se the tag so we can find it later
viewController.view.tag = SELECTED_VIEW_CONTROLLER_TAG;

// Add the new view controller's view
[self.view insertSubview:viewController.view belowSubview:self.tabBar];

//Keep track of current view controller
self.currentController = viewController;
}

到目前为止,我可以看到每个视图控制器与默认的TabBarViewController类似。但是有一个要求我需要从一个tabBar控制器内部模拟推送一个新的导航控制器(它应该占用所有的应用程序框架)。

乍一看,我在其中一个标签控制器中尝试了以下代码:

DetailViewController *detailViewController = [[DetailViewController   alloc]init];
UINavigationController *navigationController = [[UINavigationController alloc]detailViewController];
[self presentModalViewController:navigationController animated:YES];

但是没有按预期工作,首先视图显示在TabBar下面,第二个新视图没有考虑父视图框架,它应该是屏幕边界而不是tabbar。 (0,48,360,412)。我的详细信息视图控制器是从nib文件加载内容。

嗯,这很明显,因为TabBar控制器将每个视图插入我的自定义TabBar下面。

[self presentModalViewController:navigationController animated:YES];

所以我尝试将其直接插入窗口子视图:

[[UIApplication sharedApplication].keyWindow addSubview:navigationController.view];

但是,我觉得这不行......应该有一个我无法弄清楚的更好的方法。所以如果有人能给我关于如何纠正或改进这个导航系统的建议那就太棒了。

非常感谢。

1 个答案:

答案 0 :(得分:0)

如果您要为iOS 5.0及更高版本构建应用程序,则可以使用childViewController。在自定义选项卡栏中,您可以拥有一个containerView和一个tabView。

viewController的视图被添加到containerView中。如果正确实现以下方法,则会为随后添加的viewController生成所有必要的事件

- (void)addChildViewController:(UIViewController *)childController;
- (void)removeFromParentViewController;

有关viewController遏制的更多信息,请访问here.