首页 > 代码库 > 关于iOS Tabbar的一些设置

关于iOS Tabbar的一些设置

事实上iOS Tabbar的可定制性很高,我们没有必要反复造轮子,以下是笔者收集的一些tabbar的经常使用设置。希望对大家有所帮助。

设置tabbar选中颜色

iOS7设置例如以下:

[self.tabBarController.tabBarsetSelectedImageTintColor:[UIColor greenColor]];

ios8中例如以下设置:

self.tabBar.tintColor=[UIColor greenColor];


消除tabbar边框

在appdelegate的程序启动处:

[[UITabBar appearance] setShadowImage:[[UIImage alloc]init]];

[[UITabBar appearance] setBackgroundImage:[[UIImage alloc]init]];


设置tabbar item原始图标与原始选中图标。而不是系统自己主动填充的颜色

    // 拿到 TabBar 在拿到想应的item
    UITabBar *tabBar = _tabBarController.tabBar;
    UITabBarItem *item0 = [tabBar.items objectAtIndex:0];
    UITabBarItem *item1 = [tabBar.items objectAtIndex:1];
    UITabBarItem *item2 = [tabBar.items objectAtIndex:2];
    UITabBarItem *item3 = [tabBar.items objectAtIndex:3];
    // 对item设置对应地图片
    item0.selectedImage = [[UIImage imageNamed:@"recognize-1"] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];;
    item0.image = [[UIImage imageNamed:@"recognize"] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
     
    item1.selectedImage = [[UIImage imageNamed:@"life-1"] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];;
    item1.image = [[UIImage imageNamed:@"life"] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
     
    item2.selectedImage = [[UIImage imageNamed:@"edit-1"] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];;
    item2.image = [[UIImage imageNamed:@"edit"] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
     
    item3.selectedImage = [[UIImage imageNamed:@"setting-1"] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];;
    item3.image = [[UIImage imageNamed:@"setting"] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];


设置tabbar背景图片

    UIView *backView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, Main_Screen_Width, 49)];
    backView.backgroundColor = [UIColor redColor];
    [self.tabBar insertSubview:backView atIndex:0];
    self.tabBar.opaque = YES;


设置tabbar item选中时的背景图片

1、5.0以上版本号
     

self.tabBar.selectionIndicatorImage = [UIImage imageNamed:@"system_tabbar_item_selected.png"];

2、5.0下面版本号
     首先实现例如以下方法

- (void)setNoHighlistTabBar:(UITabBarController *)tabBarController
{
    NSArray * tabBarSubviews = [tabBarController.tabBar subviews];
   
    int index4SelView;
   
    if(tabBarController.selectedIndex+1 > 4)
    {//selected the last tab.
        index4SelView = [tabBarSubviews count]-1;
    }
    else if([tabBarController.viewControllers count] > 5)
    {//have "more" tab. and havn‘t selected the last tab:"more" tab.
       
       
        index4SelView = [tabBarSubviews count] - 5 + tabBarController.selectedIndex;
    }
    else
    {//have no "more" tab.
       
       
        index4SelView = [tabBarSubviews count] -
        [tabBarController.viewControllers count] + tabBarController.selectedIndex;
    }
    if([tabBarSubviews count] < index4SelView+1)
    {
        assert(false);
        return;
    }
    UIView * selView = [tabBarSubviews objectAtIndex:index4SelView];
   
    NSArray * selViewSubviews = [selView subviews];
   
    for(UIView * v in selViewSubviews)
    {
        if(v && [NSStringFromClass([v class]) isEqualToString:@"UITabBarSelectionIndicatorView"])
       
        {//the v is the highlight view.
            [self.selectedItemBgImageView removeFromSuperview];
            [selView insertSubview:self.selectedItemBgImageView belowSubview:v];
           
            [v removeFromSuperview];
           
           
            break;

        }
    }
}


  改方法的实质就是循环tabBar的subViews, 找到tabBar中的这个view, 是一个UITabBarSelectionIndicatorView的view,然后把它替换成你自己创建的UIImageView, 上例中的self.selectedItemBgImageView.
  然后须要把UITabBarController的delegate设为self, 在tabBarController:didSelectViewController的代理方法中运行上面的方法:[self setNoHighlistTabBar:self];
 还有setSelectIndex:方法中也要运行[self setNoHighlistTabBar:self];


关于iOS Tabbar的一些设置