TabBarViewController

//
//  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

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值