TabBarController 标签控制器
效果图
1.将tabbarController设置成根视图控制器
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
self.window = [[UIWindow alloc] initWithFrame:[UIScreen mainScreen].bounds];
[self.window makeKeyAndVisible];
TabBarViewController *tabbar = [[TabBarViewController alloc] init];
self.window.rootViewController = tabbar;
return YES;
}
2. 初始化子控制器
- (void)viewDidLoad {
[super viewDidLoad];
// 替换tabbar
[self setValue:[[TabBar alloc] init] forKey:@"tabBar"];
// 初始化子控制器
[self initChildViewController];
}
- (void)initChildViewController {
ViewController *one = [[ViewController alloc] init];
one.view.backgroundColor = [UIColor redColor];
[self setupChildViewController:one title:@"首页" image:@"tabBar_essence_icon" highImage:@"tabBar_essence_click_icon"];
ViewController *two = [[ViewController alloc] init];
two.view.backgroundColor = [UIColor orangeColor];
[self setupChildViewController:two title:@"朋友" image:@"tabBar_friendTrends_icon" highImage:@"tabBar_friendTrends_click_icon"];
ViewController *three = [[ViewController alloc] init];
three.view.backgroundColor = [UIColor yellowColor];
[self setupChildViewController:three title:@"我" image:@"tabBar_me_icon" highImage:@"tabBar_me_click_icon"];
ViewController *four = [[ViewController alloc] init];
four.view.backgroundColor = [UIColor greenColor];
[self setupChildViewController:four title:@"最新" image:@"tabBar_new_icon" highImage:@"tabBar_new_click_icon"];
}
- (void)setupChildViewController:(UIViewController *)vc title:(NSString *)title image:(NSString *)image highImage:(NSString *)highImage {
vc.tabBarItem = [[UITabBarItem alloc] initWithTitle:title image:[UIImage imageNamed:image] selectedImage:[UIImage imageNamed:highImage]];
[self addChildViewController:vc];
}
3. 改变底部的字体方法
+ (void)initialize {
// 获取tabbarItem的外观
UITabBarItem *appearance = [UITabBarItem appearance];
// 添加字体属性
[appearance setTitleTextAttributes:@{NSForegroundColorAttributeName:[UIColor lightGrayColor]} forState:UIControlStateNormal];
[appearance setTitleTextAttributes:@{NSForegroundColorAttributeName:[UIColor darkGrayColor]} forState:UIControlStateSelected];
}
4. 更换自定义tabBar
// 替换tabbar
[self setValue:[[TabBar alloc] init] forKey:@"tabBar"];
5. 自定义tabbar (这里用到了UIView分类,点击查看分类代码)
- (instancetype)initWithFrame:(CGRect)frame {
if (self = [super initWithFrame:frame]) {
_button = [UIButton buttonWithType:UIButtonTypeCustom];
_button.backgroundColor = [UIColor clearColor];
[_button setImage:[UIImage imageNamed:@"tabBar_publish_icon"] forState:UIControlStateNormal];
[_button setImage:[UIImage imageNamed:@"tabBar_publish_click_icon"] forState:UIControlStateHighlighted];
[self addSubview:_button];
}
return self;
}
- (void)layoutSubviews {
[super layoutSubviews];
// 设置自定义按钮的位子
[_button setFrame:CGRectMake(self.frame.size.width*2/5, 0, self.frame.size.width/5, self.frame.size.height)];
CGFloat w = self.frame.size.width / 5;
// 重新布局tabbar自身的tabbarItem的位子
NSInteger index = 0;
for (UIControl *control in self.subviews) {
if (![control isKindOfClass:[UIControl class]] || [control isKindOfClass:[UIButton class]]) continue;
control.w = w;
// 第一、二个不变,第三、四个变成第四、五个,所以索引+1
control.x = index > 1 ? w * (index + 1) : w * index;
index ++ ;
}
}
6. 解决设置了图片,图片的颜色仍然是系统颜色
原因:图片被系统渲染
解决:使用原始图片
方法一:
选中图片
选择 Original Image
方法二 :
UIImage *image = [UIImage imageNamed:@""];
// 设置图片呈现模式
[image imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];