iPhone:多个视图的同一个底栏

发布于 2025-01-04 10:22:52 字数 1299 浏览 6 评论 0原文

我想在视图中添加一个底部栏,然后在浏览多个视图时将其保留在那里(每个视图都有一个导航栏)。我希望它遵循苹果的指导方针(我已经知道只推荐nab bar)或者至少该应用程序被接受。 为此,我向应用程序窗口添加了一个 UIView。这个 UIView 包含一个 UITabBarController,其中包含该栏的每一项的导航控制器(每个作为 rootviewController):

UIWindow *window=[[UIApplication sharedApplication] keyWindow];
MyUIViewController *mv=[[MyUIViewController alloc]init];
UINavigationController *navd = [[UINavigationController alloc] initWithRootViewController:mv];
navd.tabBarItem = [[UITabBarItem alloc] initWithTitle:@"MyItem" image:[UIImage imageNamed:@"myImage.png"] tag:0];
NSMutableArray *controllers = [[NSMutableArray alloc] init];
[controllers addObject:navd];
UITabBarController *tbarController=[[UITabBarController alloc] init]; 
tbarController.viewControllers = controllers; 
tbarController.customizableViewControllers = controllers;
UIView *vistaBarraInferior=tbarController.view;
[window addSubview:vistaBarraInferior]; 
[window makeKeyAndVisible];

它可以工作,但是当我想返回并使用 UITabBarController 退出 ivie 时,我的问题出现了。如果我写:

[self.tabBarController.view removeFromSuperview];
[self.parentViewController.navigationController popViewControllerAnimated:YES];

标签栏将从当前视图中删除,但我无法到达前一个视图(我在执行任何操作之前拥有的根视图),因为我已经用“initWithRootViewController”覆盖了它。

有没有其他方法可以让事情变得更容易或让这件事成功?

提前致谢。

I would like to add a bottom bar in a view and then keep it there while I browse several views (each one with a nab bar). I hope it will follow Apple guidelines (I already know that only the nab bar is recommended) or at least the app is accepted.
For doing so, I have added a UIView to the application window. This UIView contains a UITabBarController which contains the navigation controllers (each one as a rootviewController) of each one of the items of the bar:

UIWindow *window=[[UIApplication sharedApplication] keyWindow];
MyUIViewController *mv=[[MyUIViewController alloc]init];
UINavigationController *navd = [[UINavigationController alloc] initWithRootViewController:mv];
navd.tabBarItem = [[UITabBarItem alloc] initWithTitle:@"MyItem" image:[UIImage imageNamed:@"myImage.png"] tag:0];
NSMutableArray *controllers = [[NSMutableArray alloc] init];
[controllers addObject:navd];
UITabBarController *tbarController=[[UITabBarController alloc] init]; 
tbarController.viewControllers = controllers; 
tbarController.customizableViewControllers = controllers;
UIView *vistaBarraInferior=tbarController.view;
[window addSubview:vistaBarraInferior]; 
[window makeKeyAndVisible];

It works, but my problem appears when I would like to go back and exit the ivies with UITabBarController. If I write:

[self.tabBarController.view removeFromSuperview];
[self.parentViewController.navigationController popViewControllerAnimated:YES];

The tab bar is removed from the current view, but I can´t reach the previous view (the root view I had had before doing anything), because I have overwritten it with the 'initWithRootViewController'.

Is it any other way to make it easier or make this work out?

Thanks in advance.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

晚雾 2025-01-11 10:22:52

如果要组合选项卡栏和导航栏,则必须使用选项卡栏作为根,并为每个选项卡项添加一个导航控制器。您不能以相反的方式执行此操作,因为选项卡栏必须始终为根。

我不确定你想用底栏实现什么目标。它是否应用作选项卡栏或工具栏。请记住,如果您使用选项卡栏,那么每个选项卡将有自己的由导航控制器管理的视图堆栈。另一方面,工具栏与一个视图相关。

如果您想创建类似覆盖工具栏的东西 - 即使它们以动画方式进出,也始终位于视图之上 - 您需要选择不同的解决方案。

例如,您可以创建自己的控制器,该控制器将在底部显示工具栏和一些容器视图,其内容将由导航控制器管理。

If you want to combine tab bar and navigation bar you must use tab bar as a root and for each tab item add one navigation controller. You can't do it in opposite way as tab bar must be always root.

I am not sure what do you want achieve with bottom bar. Whether it should serve as a tab bar or toolbar. Remember if you use tab bar then each tab will have its own stack of views managed by navigation controller. On the other hand toolbar is related to one view.

If you want to create something like overlay toolbar - something that is always on top of views even if they are animating in and out - you need to choose different solution.

For example you can create your own controller that will display your toolbar at bottom and some container view which content will be managed by navigation controller.

还给你自由 2025-01-11 10:22:52

您应该使用带有导航控制器数组的选项卡控制器。请参阅苹果示例代码 例如这里

我浏览了你的代码,它在不同级别上看起来不正确。例如,您正在调用 initwithviewcontroller 但传递 uiview。这是不正确的。

You should use a tabbarcontroller with array of navigation controllers. See apple sample code here for example.

I went through your code and it does not look right on different levels. For example you are calling initwithviewcontroller but passing a uiview. That is not correct.

岁吢 2025-01-11 10:22:52

如果我用其他方式重写这段代码:

        UITabBarController *tabBar=[[UITabBarController alloc]init];
        tabBar.title=@"MyTitle";
        NSMutableArray *items=[[NSMutableArray alloc]init];
        MyUIViewController *ld = [[MyUIViewController alloc] init];
        [tabBar addChildViewController:ld];            
        UITabBarItem *tabItem=[[UITabBarItem alloc]initWithTitle:@"Item1" image:[UIImage imageNamed:@"myImage.png"] tag:0];
        [items addObject:tabItem];
        [tabItem release];
        [tabBar setToolbarItems:items];
        [self.navigationController pushViewController:tabBar animated:YES];

结果是底部的标签栏是空的。我是否忘记写任何其他内容来显示选项卡栏项目?

如果我添加第二个项目,则仅显示其标题,但不会显示其图像或与第一个项目相关的任何内容。

If I rewrite this code in other way:

        UITabBarController *tabBar=[[UITabBarController alloc]init];
        tabBar.title=@"MyTitle";
        NSMutableArray *items=[[NSMutableArray alloc]init];
        MyUIViewController *ld = [[MyUIViewController alloc] init];
        [tabBar addChildViewController:ld];            
        UITabBarItem *tabItem=[[UITabBarItem alloc]initWithTitle:@"Item1" image:[UIImage imageNamed:@"myImage.png"] tag:0];
        [items addObject:tabItem];
        [tabItem release];
        [tabBar setToolbarItems:items];
        [self.navigationController pushViewController:tabBar animated:YES];

The result is that the tab bar at the bottom is empty. Have I forgotten to write anything else to show the tab bar item?

If I add a second item, only its title is shown, but neither its image nor anything related to the first item is shown.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文