【问题标题】:How to set Tabbar integration in iPhone?如何在 iPhone 中设置 Tabbar 集成?
【发布时间】:2012-09-13 08:08:32
【问题描述】:

我正在开发一个 iPhone 应用程序,使用 UITabBar 来开发它。标签栏中只有第二个标签栏标题可见,但第一个标签栏标题不显示在标签栏中,如何解决这个问题?请帮我, 提前致谢。

以下是源代码供您参考。

NSMutableArray *items = [[NSMutableArray alloc]init];
tab =[[UITabBar alloc]initWithFrame:CGRectMake(0, 300, 320, 44)];

[self.tabBarItem initWithTitle:@"sample1" image:nil tag:111];
[items addObject:self.tabBarItem];

[self.tabBarItem initWithTitle:@"sample2" image:nil tag:101];
[items addObject:self.tabBarItem];

[tab setItems:items animated:YES]; 
[self.view addSubview:tab];

【问题讨论】:

  • 你最好为每个标签设置一张图片。
  • 我试过这个来导入图像但是警告来了,如何设置系统项目图像? [self.tabBarItem initWithTitle:@"3" image:UITabBarSystemItemFeatured tag:11];

标签: iphone ios title tabbar


【解决方案1】:

不要浏览视图控制器,因为那样你的包含 TabBar 的控制器会被弹出。

当我们使用 UITabBar 时,我们不会以这种方式使用导航。

只需 addSubView 带有此类框架的新控制器,以便您的 TabBar 始终可见。

【讨论】:

    【解决方案2】:

    您必须在 UITabBar 中添加该 tabbarItem。

       NSArray *items = [[NSArray alloc]init];
        [items addObject:self.tabBarItem]; //add all your tabBarItems in an array
        [tab setItems:items animated:YES]; //then set all your tabBarItems in UITabBar like this
    

    【讨论】:

    • 我试过了,但现在标签栏背景在屏幕上也不可见
    • 对不起我的错,现在我尝试了这种方式 NSMutableArray *items = [[NSMutableArray alloc]init]; tab =[[UITabBar alloc]initWithFrame:CGRectMake(0, 300, 320, 44)]; [self.tabBarItem initWithTitle:@"sample1" image:nil tag:11]; [项目添加对象:self.tabBarItem]; [self.tabBarItem initWithTitle:@"sample2" image:nil tag:11]; [项目添加对象:self.tabBarItem]; [选项卡 setItems:动画项目:是]; [self.view addSubview:tab];
    • 标签栏只显示第二个标签项,第一个标签项标题不显示,请帮帮我
    • Hey Minu,您使用的是相同的 tabbarItem 实例,最后只添加了一个 barItem。检查下面我在另一个答案中添加了代码,它会帮助你。
    【解决方案3】:

    实际上,您使用的是相同的 tabBarItem 实例,也就是说您只获得了第二个按钮。 这样做,如果这对您有帮助,请接受答案。

    UITabBar *tabBar = [[UITabBar alloc]initWithFrame:CGRectMake(0, 725, 768, 49)];
        NSMutableArray *items = [[NSMutableArray alloc]init];
        UITabBarItem *item1 = [[UITabBarItem alloc]initWithTitle:@"first" image:nil tag:1];
        UITabBarItem *item2 = [[UITabBarItem alloc]initWithTitle:@"second" image:nil tag:2];
        UITabBarItem *item3 = [[UITabBarItem alloc]initWithTitle:@"third" image:nil tag:3];
        UITabBarItem *item4 = [[UITabBarItem alloc]initWithTitle:@"fourth" image:nil tag:4];
    
        [items addObject:item1];
        [items addObject:item2];
        [items addObject:item3];
        [items addObject:item4];
    
        [tabBar setItems:items];
        [self.view addSubview:tabBar];
    

    【讨论】:

    • 我还有一个问题?
    • 如何在所有选项卡中连接 ViewController,你能帮帮我吗
    • Mnu 试试这个,它是UITabBar的一个委托方法。 - (void)tabBar:(UITabBar *)tabBar didSelectItem:(UITabBarItem *)item{ }
    • 我已经尝试过这个 deepak。 - (void)tabBar:(UITabBar *)tabBar didSelectItem:(UITabBarItem *)item { NSLog(@"Tag:%d",item.tag); if(item.tag = 100) { Screen1 *s1 =[[screen1 alloc]init]; [导航 pushViewController:s1 动画:YES];但是当我按下第一个屏幕时,它会转到 screen1 但屏幕上看不到标签栏,如何解决这个问题?你能举个简单的例子吗,我只希望你deepak
    • 其实这是一个完美的答案,应该是一个可以接受的答案。当前接受的答案与问题无关。在 stackoverflow 中应该只接受完美的答案。
    【解决方案4】:

    这不是您以编程方式创建和添加 TabBar 的方式。 您宁愿使用其他方法:https://stackoverflow.com/a/3844365/655221

    祝你好运。

    【讨论】:

    • 你说对了@ale0xB。当您使您的应用程序完全基于 TabBar 时,将使用您建议的方式。以上解决方案仅适用于您只想将标签栏添加到应用程序的某些部分。谢谢
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2020-05-19
    • 2021-07-11
    • 2021-08-20
    • 2021-02-28
    • 2011-02-25
    • 2012-01-21
    • 2011-12-10
    相关资源
    最近更新 更多