UITabBar即选项卡,使用它比较简单,主要是创建UITabBarItem,然后添加到UITabBar的items里。
1、UITabBar的创建
UITabBar *tabBar = [[UITabBaralloc]initWithFrame:CGRectMake(0,420, 320,60)];
2、创建UITabBarItem
有两种方法:
initWithTabBarSystemItem、initWithTitle
这里用initWithTabBarSystemItem作演示
UITabBarItem *tabBarItem01 = [[UITabBarItemalloc]initWithTabBarSystemItem:UITabBarSystemItemDownloadstag:1];
UITabBarItem *tabBarItem02 = [[UITabBarItemalloc]initWithTabBarSystemItem:UITabBarSystemItemHistorytag:2];
UITabBarItem *tabBarItem03 = [[UITabBarItemalloc]initWithTabBarSystemItem:UITabBarSystemItemContactstag:3];
UITabBarItem *tabBarItem04 = [[UITabBarItemalloc]initWithTabBarSystemItem:UITabBarSystemItemMoretag:4];
系统的UITabBarItem的样式主要有以下几种:
typedef NS_ENUM(NSInteger, UITabBarSystemItem) {
UITabBarSystemItemMore,
UITabBarSystemItemFavorites,
UITabBarSystemItemFeatured,
UITabBarSystemItemTopRated,
UITabBarSystemItemRecents,
UITabBarSystemItemContacts,
UITabBarSystemItemHistory,
UITabBarSystemItemBookmarks,
UITabBarSystemItemSearch,
UITabBarSystemItemDownloads,
UITabBarSystemItemMostRecent,
UITabBarSystemItemMostViewed,
};
3、将UITabBarItems添加
NSMutableArray * tabBarItemArray = [NSMutableArrayarrayWithObjects:tabBarItem01,tabBarItem02,tabBarItem03,tabBarItem04,tabBarItem05,tabBarItem06,nil];
[tabBar setItems:tabBarItemArrayanimated:YES];
4、设置UITabBar的属性
tabBar.tintColor = [UIColorredColor];
5、添加delegate,并实现delegate的方法
tabBar.delegate =self;
- (void)tabBar:(UITabBar *)tabBar didSelectItem:(UITabBarItem *)item
{
NSInteger tag = item.tag;
NSString * tagString = [NSStringstringWithFormat:@"您选择了%d个tabBarItem",tag];
UIAlertView * alertView = [[UIAlertViewalloc]initWithTitle:@"选择了tabBarItem"message:tagString delegate:selfcancelButtonTitle:@"取消"otherButtonTitles:@"确定",nil];
[alertView show];
NSLog(@"您选择的tag是:%d",tag);
}
详细demo见:https://github.com/tingxuan/TXUITabBarDemo