//
// MyTabBarViewController.m
// Social
//
// Created by Mac on 2020/10/20.
// Copyright © 2020 Mac. All rights reserved.
//
#import "MyTabBarViewController.h"
@interface MyTabBarViewController ()
@property (nonatomic, weak) UIButton *composeButton;
@end
@implementation MyTabBarViewController
- (void)viewWillAppear:(BOOL)animated {
[super viewWillAppear:animated];
// 不置前点击中间按钮会导致黑屏
[self.tabBar bringSubviewToFront:self.composeButton];
}
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view.
}
#pragma mark - 初始化
- (instancetype)init {
if (self = [super init]) {
[self addChildViewControllers];
[self addComposeButton];
}
return self;
}
#pragma mark - 添加所有子控制器
- (void)addChildViewControllers {
// [self addChildViewController:[[MainViewController alloc]init] withTitle:@"首页" withNormalImage:@"" withSelectImage:@""];
//
// [self addChildViewController:[[CircleViewController alloc]init] withTitle:@"社区" withNormalImage:@"" withSelectImage:@""];
//
// [self addChildViewController:[[MsgViewController alloc]init] withTitle:@"消息" withNormalImage:@"" withSelectImage:@""];
//
// [self addChildViewController:[[MineViewController alloc]init] withTitle:@"我的" withNormalImage:@"" withSelectImage:@""];
[self addChildViewControllerClassName:@"MainViewController" title:@"首页" imageName:@"main"];
[self addChildViewControllerClassName:@"CircleViewController" title:@"社区" imageName:@"area"];
[self addChildViewController: [[UIViewController alloc] init]];
[self addChildViewControllerClassName:@"MsgViewController" title:@"消息" imageName:@"msg"];
[self addChildViewControllerClassName:@"MineViewController" title:@"我的" imageName:@"mine"];
self.tabBar.tintColor = RGB(254, 195, 10, 1);
}
#pragma mark - 设置所有子控制器
- (void)addChildViewControllerClassName:(NSString *)className title:(NSString *)title imageName:(NSString *)imageName
{
UIViewController *viewController = [[NSClassFromString(className) alloc] init];
UINavigationController *nav = [[UINavigationController alloc] initWithRootViewController:viewController];
viewController.title = title;
viewController.view.backgroundColor = [UIColor whiteColor];
viewController.tabBarItem.image = [UIImage imageNamed: imageName];
viewController.tabBarItem.selectedImage = [UIImage imageNamed:[NSString stringWithFormat:@"%@_select", imageName]];
[self addChildViewController:nav];
}
#pragma mark - 添加中间的按钮
- (void)addComposeButton {
UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
[button setImage:[UIImage imageNamed:@"add_select"] forState:UIControlStateNormal];
[button setImage:[UIImage imageNamed:@"add_select"] forState:UIControlStateSelected];
[button addTarget:self action:@selector(composeButtonClick:) forControlEvents:UIControlEventTouchUpInside];
self.composeButton = button;
[self.tabBar addSubview:button];
[self.tabBar bringSubviewToFront:button];
CGFloat width = self.tabBar.bounds.size.width / self.childViewControllers.count - 2;
// self.tabBar.tintColor = [UIColor colorWithRed:68/255.0 green:173/255.0 blue:159/255.0 alpha:1];
button.frame = CGRectInset(self.tabBar.bounds, 2 * width, 0);
}
#pragma mark - 点击写文章按钮
- (void)composeButtonClick:(UIButton *)button {
NSLog(@"\n点击了按钮");
}
#pragma mark - 统一设置所有 UITabBarItem 的文字属性
+ (void)initialize {
NSMutableDictionary *attrs = [NSMutableDictionary dictionary];
attrs[NSFontAttributeName] = [UIFont systemFontOfSize:12];
attrs[NSForegroundColorAttributeName] = [UIColor grayColor];
NSMutableDictionary *selectedAttrs = [NSMutableDictionary dictionary];
selectedAttrs[NSFontAttributeName] = attrs[NSFontAttributeName];
selectedAttrs[NSForegroundColorAttributeName] = RGB(254, 195, 10, 1);
UITabBarItem *items = [UITabBarItem appearance];
[items setTitleTextAttributes:attrs forState:UIControlStateNormal];
[items setTitleTextAttributes:selectedAttrs forState:UIControlStateSelected];
}
/*
#pragma mark - Navigation
// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
// Get the new view controller using [segue destinationViewController].
// Pass the selected object to the new view controller.
}
*/
@end