如何以编程方式添加UITabBarItem? [英] how to add UITabBarItem programmatically?

查看:101
本文介绍了如何以编程方式添加UITabBarItem?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的应用程序中,我在"Storyborad"中添加了UITabBarController. 但是现在我想以编程方式添加UITabBarItem.假设有5个按钮,每当用户单击该按钮时,我的UITabBarController就会被调用. 标签栏"中共有5个标签. 例如:

In my application, i have added UITabBarController in "Storyborad". But now i want to add UITabBarItem programmatically. Let's say there are 5 buttons and when ever user click on that button my UITabBarController is called. There are total 5 tab in "tabbar". e.g:

标签名称:item1,item2,item3,item4,item5. 按钮名称:item1,item2,item3,item4,item5.

Tab name:item1,item2,item3,item4,item5. Button name:item1,item2,item3,item4,item5.

比方说,用户单击item1按钮,将调用UITabBarcontroller.现在,用户应该不能在"tabbarcontroller"中看到item1选项卡,而应该可以看到重置的"tabBaritem".

Let's say user click on item1 button, UITabBarcontroller is called. Now user should not be able to see that item1 tab in "tabbarcontroller", he should be able to see the reset "tabBaritem".

有人可以帮我吗? 如何以编程方式进行?

Can anyone help me out with this? How to do it programmatically?

谢谢.

Nilesh J

推荐答案

UITabBarItem * itemNew = [[UITabBarItem alloc] initWithTitle:@"Page 1"
                                                     image:[UIImage imageNamed:@"page1_image_normal"]
                                             selectedImage:[UIImage imageNamed:@"page1_image_selected"]];

获取现有的tabBarItems

NSMutableArray *tbItems = [NSMutableArray arrayWithArray:[self.tabBar items]];
//Add your new tabBarItem
[tbItems addObject:itemNew];

//Set your tabBar items to the new array
[self.tabBar setItems:tbItems];

快捷键4

if var controllers = tabBarController?.viewControllers {
   let tabItem = UITabBarItem(title: "Item \(controllers.count)", image: nil, selectedImage: nil)
   let vc = UIViewController() // your new view controller
   vc.tabBarItem = tabItem
   controllers.append(vc) // or insert at index up to you
   tabBarController?.setViewControllers(controllers, animated: true)
}

这篇关于如何以编程方式添加UITabBarItem?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆