【问题标题】:Custom TabBar navigation issues自定义 TabBar 导航问题
【发布时间】:2013-03-05 15:11:19
【问题描述】:

我将自定义标签栏控制器实现为一组按钮,每个按钮都与它自己的视图控制器相关。我在此链接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 下方,其次新视图没有考虑父视图框架,它应该是屏幕边界减去标签栏。 (0、48、360、412)。我的详细视图控制器正在从 nib 文件加载内容。

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

[self presentModalViewController:navigationController animated:YES];

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

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

但是,我认为这不太好……应该有更好的方法,我想不出。因此,如果有人能就如何纠正或改进这个导航系统给我建议,那就太好了。

非常感谢。

【问题讨论】:

    标签: iphone ios uinavigationcontroller uitabbarcontroller


    【解决方案1】:

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

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

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

    更多关于 viewController 的遏制可以在here. 找到

    【讨论】:

    • 谢谢,很遗憾我需要支持 4.x 设备。
    • 如果您有权做出决定,您可以选择 5.0 的最低目标,因为这使得视图控制器包含非常容易。
    猜你喜欢
    • 2019-05-08
    • 1970-01-01
    • 2018-06-15
    • 2021-10-17
    • 1970-01-01
    • 1970-01-01
    • 2015-07-15
    • 2013-02-17
    • 1970-01-01
    相关资源
    最近更新 更多