我有应用程序,我已动态创建标签栏。现在我想添加默认项目,如联系人,更多,关于,收藏等。我如何使用Tab-Bar动态添加所有这些项目?
CGRect myTab =CGRectMake(0,368,320,49);
UITabBar *tabBar = [[UITabBar alloc] initWithFrame:myTab];
[self.view addSubview:tabBar];
答案 0 :(得分:3)
通常你会使用UITabBarController创建一个TabBar,在这种情况下你可以简单地设置属性
@property(nonatomic, copy) NSArray *viewControllers
如果您确定要创建UITabBar,则需要使用items属性。像这样:
- (void) setupTabBar {
UITabBar *tabBar = [[UITabBar alloc] initWithFrame:myTab];
NSMutableArray *items = [[[NSMutableArray alloc] init] autorelease];
// Add a 'test' item with no image and the text "Test"
[items addObject:[[[UITabBarItem alloc] initWithTitle:@"Test" image:nil tag:1] autorelease] ];
// Add a 'contacts' item
[items addObject:[[[UITabBarItem alloc] initWithTabBarSystemItem:UITabBarSystemItemContacts tag:2] autorelease] ];
// Put the items in the tab bar
tabBar.items = items;
// Setup this object to respond to tab changes
tabBar.delegate = self;
}
- (void)tabBar:(UITabBar *)tabBar didSelectItem:(UITabBarItem *)item {
if (item.tag == 2) {
// Contacts was selected. Do something...
}
}