美文网首页知识点iOS知识IOS
iOS 自定义TabBarItem,图片加文字,颜色自定义

iOS 自定义TabBarItem,图片加文字,颜色自定义

作者: Learning_ | 来源:发表于2016-05-30 09:22 被阅读1485次

    [[UITabBarItem appearance]setTitleTextAttributes:@{NSFontAttributeName:[UIFont systemFontOfSize:12.0f],NSForegroundColorAttributeName:[UIColor colorWithRed:1.000green:0.314blue:0.000alpha:1]}forState:UIControlStateSelected];

    HomeViewController*homeVC = [[HomeViewController alloc]initWithNibName:nil bundle:nil];

    homeVC.tabBarItem= [[UITabBarItem alloc]initWithTitle:@"商城"image:[UIImage imageNamed:@"menu_home_off"]selectedImage:[self originalImageName:@"menu_home_on"]];

    UINavigationController*homeNavigation = [[UINavigationController alloc]initWithRootViewController:homeVC];

    LocalViewController*localVC = [[LocalViewController alloc]initWithNibName:nil bundle:nil];

    localVC.tabBarItem= [[UITabBarItem alloc]initWithTitle:@"本地"image:[UIImage imageNamed:@"menu_merchant_off"]selectedImage:[self originalImageName:@"menu_merchant_on"]];

    UINavigationController*localNavigation = [[UINavigationController alloc]initWithRootViewController:localVC];

    ShoppingCarViewController*shoppingCarVC = [[ShoppingCarViewController alloc]initWithNibName:nil bundle:nil];

    shoppingCarVC.tabBarItem= [[UITabBarItem alloc]initWithTitle:@"购物车"image:[UIImage imageNamed:@"menu_cart_off"]selectedImage:[self originalImageName:@"menu_cart_on"]];

    UINavigationController*shoppingCarNavigation = [[UINavigationController alloc]initWithRootViewController:shoppingCarVC];

    MyViewController*myVC = [[MyViewController alloc]initWithNibName:nil bundle:nil];

    myVC.tabBarItem= [[UITabBarItem alloc]initWithTitle:@"我的"image:[UIImage imageNamed:@"menu_account_off"]selectedImage:[self originalImageName:@"menu_account_on"]];

    UINavigationController*myNavigation = [[UINavigationController alloc]initWithRootViewController:myVC];

    UITabBarController*tabBarController = [[UITabBarController alloc]initWithNibName:nil bundle:nil];

    tabBarController.viewControllers=@[homeNavigation,localNavigation,shoppingCarNavigation,myNavigation];

    [UIApplication sharedApplication].delegate.window.rootViewController= tabBarController;

    封装一个返回image的方法,用来设置tabBarItem的背景图片,使用原始图像

    - (UIImage*)originalImageName:(NSString*)imageName

    {

    UIImage *img = [UIImage imageNamed:imageName];

    return [img imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];

    }

    设置tabbarItem下方文字被选中时的颜色

    [[UITabBarItem appearance]setTitleTextAttributes:@{NSFontAttributeName:[UIFont systemFontOfSize:12.0f],NSForegroundColorAttributeName:[UIColor colorWithRed:1.000green:0.314blue:0.000alpha:1]}forState:UIControlStateSelected];

    效果如图:

    相关文章

      网友评论

      • 尼古拉斯洪:[[UITabBarItem appearance]setTitleTextAttributes:@{NSFontAttributeName:[UIFont systemFontOfSize:12.0f],NSForegroundColorAttributeName:[UIColor colorWithRed:1.000green:0.314blue:0.000alpha:1]}forState:UIControlStateSelected];
        我的工程里也是这么写的,但是改变字体大小没变化啊,
      • 无书不欢胡:好
        Learning_:@无书不欢胡 共同学习

      本文标题:iOS 自定义TabBarItem,图片加文字,颜色自定义

      本文链接:https://www.haomeiwen.com/subject/bcgqdttx.html