1.
使用自定义方式定义
tabbarItem
UITabBarItem *item1 = [[ UITabBarItem alloc ] initWithTabBarSystemItem : UITabBarSystemItemFavorites tag : 1 ];
item1. badgeValue = @"item1" ;
homeVC. tabBarItem = item1;
UITabBarItem *item2 = [[ UITabBarItem alloc ] initWithTabBarSystemItem : UITabBarSystemItemBookmarks tag : 2 ];
item2. badgeValue = @"haha" ;
UITabBarItem *item1 = [[ UITabBarItem alloc ] initWithTabBarSystemItem : UITabBarSystemItemFavorites tag : 1 ];
item1. badgeValue = @"item1" ;
homeVC. tabBarItem = item1;
UITabBarItem *item2 = [[ UITabBarItem alloc ] initWithTabBarSystemItem : UITabBarSystemItemBookmarks tag : 2 ];
item2. badgeValue = @"haha" ;
messageVC.tabBarItem = item2;
2.
设置
tabbar
的背景颜色
tabCtrl.tabBar.barTintColor = [UIColor cyanColor];
3.设置选中图片的颜色
tabCtrl.
tabBar
.
tintColor
= [
UIColor
cyanColor
];
4.设置选中
item
后,显示在此
item
下面的图片
tabCtrl.tabBar.selectionIndicatorImage = [UIImageimageNamed:@"选中.png"];
5.
隐藏自己的
tabbarView
self.tabBar.hidden = true;
6.
创建选中视图
UIImageView *selectedView = [[ UIImageView alloc ] initWithFrame : CGRectMake ( 0 , 0 , 53 , 45 )];
selectedView. tag = 100 ;
UIImageView *selectedView = [[ UIImageView alloc ] initWithFrame : CGRectMake ( 0 , 0 , 53 , 45 )];
selectedView. tag = 100 ;
selectedView.image = [UIImageimageNamed:@"选中.png"];
7.创建标签栏的5个按钮
for
(
int
i =
0
; i <
5
; i++) {
UIButton *btn = [ UIButton buttonWithType : UIButtonTypeCustom ];
btn. tag = 200 + i;
btn. frame = CGRectMake (scrrenWidth / 5 * i, 0 , scrrenWidth / 5 , 49 );
[btn addTarget : self action : @selector (btnClick:) forControlEvents : UIControlEventTouchUpInside ];
NSString *imgName = [ NSString stringWithFormat : @"%d.png" , i + 1 ];
[btn setImage :[ UIImage imageNamed :imgName] forState : UIControlStateNormal ];
UIButton *btn = [ UIButton buttonWithType : UIButtonTypeCustom ];
btn. tag = 200 + i;
btn. frame = CGRectMake (scrrenWidth / 5 * i, 0 , scrrenWidth / 5 , 49 );
[btn addTarget : self action : @selector (btnClick:) forControlEvents : UIControlEventTouchUpInside ];
NSString *imgName = [ NSString stringWithFormat : @"%d.png" , i + 1 ];
[btn setImage :[ UIImage imageNamed :imgName] forState : UIControlStateNormal ];
[_tabbarViewaddSubview:btn];
//设置选中图片中心与btn图片中心相同
if
(i ==
0
) {
selectedView. center = btn. center ;
}
selectedView. center = btn. center ;
}
}
- (
void
)btnClick:(
UIButton
*)btn
{
// 设置选择的控制器
self . selectedIndex = btn. tag - 200 ;
UIView *selectedView = [ _tabbarView viewWithTag : 100 ];
[ UIView animateWithDuration : 0.3
animations :^{
selectedView. center = btn. center ;
{
// 设置选择的控制器
self . selectedIndex = btn. tag - 200 ;
UIView *selectedView = [ _tabbarView viewWithTag : 100 ];
[ UIView animateWithDuration : 0.3
animations :^{
selectedView. center = btn. center ;
}];
}
8.
- (
void
)_initViewControllers
{
//
三级控制器
HomeViewController *homeVC = [[ HomeViewController alloc ] init ];
MessageViewController *messageVC = [[ MessageViewController alloc ] init ];
SearchViewController *searchVC = [[ SearchViewController alloc ] init ];
SettingViewController *settingVC = [[ SettingViewController alloc ] init ];
MoreViewController *moreVC = [[ MoreViewController alloc ] init ];
NSArray *viewControllers = @[ homeVC, messageVC, searchVC, settingVC, moreVC ] ;
NSMutableArray *navs = [ NSMutableArray array ];
// 二级控制器
for ( int i = 0 ; i < viewControllers. count ; i ++) {
UINavigationController *nav = [[ UINavigationController alloc ] initWithRootViewController :viewControllers[i]];
nav. delegate = self ;
[navs addObject :nav];
}
// 一级控制器
self . viewControllers = navs;
HomeViewController *homeVC = [[ HomeViewController alloc ] init ];
MessageViewController *messageVC = [[ MessageViewController alloc ] init ];
SearchViewController *searchVC = [[ SearchViewController alloc ] init ];
SettingViewController *settingVC = [[ SettingViewController alloc ] init ];
MoreViewController *moreVC = [[ MoreViewController alloc ] init ];
NSArray *viewControllers = @[ homeVC, messageVC, searchVC, settingVC, moreVC ] ;
NSMutableArray *navs = [ NSMutableArray array ];
// 二级控制器
for ( int i = 0 ; i < viewControllers. count ; i ++) {
UINavigationController *nav = [[ UINavigationController alloc ] initWithRootViewController :viewControllers[i]];
nav. delegate = self ;
[navs addObject :nav];
}
// 一级控制器
self . viewControllers = navs;
}