博客> 自定义UITabBar的图片
自定义UITabBar的图片
2020-01-21 15:48 评论:0 阅读:552 superjunjun
UINavgationController 自定义UITabBar

在做项目的时候发现,在使用storyBoard的时候设置tabbar使用ui给的图时,tabBar的图片在选中状态的时候 会自己根据tintColor渲染。

有两个方法解决,

一个是在倒入图片的时候使用xcode自己的图片集Assets.xcassets 在设置Render As Oriminal Image(始终绘制图片原始状态,不使用Tint Color)Template Image(始终根据Tint Color绘制图片,忽略图片的颜色信息)

第二种就是自定义UITabBarController

// // ISGBaseTabBarController.m // iShanggang // // Created by lijun on 2017/4/20. // Copyright © 2017年 aishanggang. All rights reserved. //

import "ISGBaseTabBarController.h"

@interface ISGBaseTabBarController ()

@end

@implementation ISGBaseTabBarController

  • (void)viewDidLoad { [super viewDidLoad];

    [self customTabBarLoad]; }

  • (void)customTabBarLoad { UITabBar tabBar = self.tabBar; tabBar.barStyle = UIBarStyleBlack; tabBar.translucent = NO; UIColor tintColor = [UIColor whiteColor]; tabBar.tintColor = tintColor; if([tabBar respondsToSelector:@selector(barTintColor)]) { tabBar.barTintColor = tintColor; }

    UITabBarItem item0 = [tabBar.items objectAtIndex:0]; UITabBarItem item1 = [tabBar.items objectAtIndex:1]; UITabBarItem item2 = [tabBar.items objectAtIndex:2]; UITabBarItem item3 = [tabBar.items objectAtIndex:3]; UITabBarItem *item4 = [tabBar.items objectAtIndex:4];

    // 对item设置相应地图片 item0.selectedImage = [[UIImage imageNamed:@"hp_home_s"] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];; item0.image = [[UIImage imageNamed:@"hp_home_n"] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];

    item1.selectedImage = [[UIImage imageNamed:@"rh_hall_s"] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];; item1.image = [[UIImage imageNamed:@"rh_hall_n"] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];

    item2.selectedImage = [[UIImage imageNamed:@"im_mall_s"] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];; item2.image = [[UIImage imageNamed:@"im_mall_n"] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];

    item3.selectedImage = [[UIImage imageNamed:@"news_new_s"] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];; item3.image = [[UIImage imageNamed:@"news_new_n"] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];

    item4.selectedImage = [[UIImage imageNamed:@"mine_me_s"] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];; item4.image = [[UIImage imageNamed:@"mine_me_n"] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];

    [item0 setTitleTextAttributes:@{NSForegroundColorAttributeName :cCommonRedColor} forState:UIControlStateSelected]; [item1 setTitleTextAttributes:@{NSForegroundColorAttributeName :cCommonRedColor} forState:UIControlStateSelected]; [item2 setTitleTextAttributes:@{NSForegroundColorAttributeName :cCommonRedColor} forState:UIControlStateSelected]; [item3 setTitleTextAttributes:@{NSForegroundColorAttributeName :cCommonRedColor} forState:UIControlStateSelected]; [item4 setTitleTextAttributes:@{NSForegroundColorAttributeName :cCommonRedColor} forState:UIControlStateSelected]; }

@end

还有一个就是nav的透明度 会导致色值有差 自定义UINavgationController { [super viewDidLoad];

self.navigationBar.barStyle = UIBarStyleBlack;
self.navigationBar.translucent = NO;
//半透明设为NO
UIColor *tintColor = cCommonRedColor;
self.navigationBar.tintColor = tintColor;
if([self.navigationBar respondsToSelector:@selector(barTintColor)])
{
    self.navigationBar.barTintColor = tintColor;
}

UIFont *titleTextFont = [BOAssistor defaultTextStringFontWithSize:19.0];
self.navigationBar.titleTextAttributes = @{NSFontAttributeName:titleTextFont};

}

收藏
0
sina weixin mail 回到顶部