自定义导航栏 tabBarController 笔记

#import "LeeNavigationController.h"

 

@interface LeeNavigationController ()

 

@end

 

@implementation LeeNavigationController

 

+(void)initialize

{

    // Attributes 属性

    NSDictionary  *textAttributes=@{NSForegroundColorAttributeName:[UIColor whiteColor],NSFontAttributeName:[UIFont systemFontOfSize:25]};

    // 设置导航栏的字体大小  颜色

    UINavigationBar *navBar = [UINavigationBar appearance];

    [navBar setTitleTextAttributes:textAttributes];

    [navBar setBarTintColor:RGB_COLOR(80, 135, 251)];

    

    

    NSDictionary  *textAttributes2=@{NSForegroundColorAttributeName:[UIColor whiteColor],NSFontAttributeName:[UIFont systemFontOfSize:20]};

    UIBarButtonItem *item = [UIBarButtonItem appearance];

    [item setTitleTextAttributes:textAttributes2 forState:UIControlStateNormal];

    [item setTintColor:[UIColor whiteColor]];

    

 

}

 

#import <UIKit/UIKit.h>

 

@interface LeeTabBarController : UITabBarController

 

+ (instancetype)configTabBarController;

@end

 

#import "LeeTabBarController.h"

#import "oneViewController.h"

#import "twoViewController.h"

#import "threeViewController.h"

#import "LeeNavigationController.h"

#import "LeeButton.h"

@interface LeeTabBarController ()

@property (nonatomic, strong)NSMutableArray *buttonDatas;

@property (nonatomic, strong)NSMutableArray *buttonArray;

@property (nonatomic, weak)UIImageView *backImageView;

@end

 

@implementation LeeTabBarController

 

+ (instancetype)configTabBarController

{

    return [[self alloc]init];

}

- (void)viewDidLoad {

    [super viewDidLoad];

    // Do any additional setup after loading the view.

    //隐藏掉自带的tabBar

    self.tabBar.hidden = YES;

    

    //初始化按钮的文字 图片

    [self initButtonData];

    

    //添加tabBar

    [self initTabBar];

    

    //添加子控制器

    [self addChildControllers];

}

 

- (void)initButtonData

{

    _buttonDatas = [NSMutableArray array];

    NSDictionary *dic1 = [NSDictionary dictionaryWithObjectsAndKeys:@"电影",@"title",@"record_unselected",@"norImage",@"record_selected",@"selImage" ,nil];

    NSDictionary *dic2 = [NSDictionary dictionaryWithObjectsAndKeys:@"影院",@"title",@"record_unselected",@"norImage",@"record_selected",@"selImage" ,nil];

    NSDictionary *dic3 = [NSDictionary dictionaryWithObjectsAndKeys:@"新闻",@"title",@"record_unselected",@"norImage",@"record_selected",@"selImage" ,nil];

//    NSDictionary *dic4 = [NSDictionary dictionaryWithObjectsAndKeys:@"社区",@"title",@"",@"norImage",@"",@"secImage" ,nil];

//    NSDictionary *dic5 = [NSDictionary dictionaryWithObjectsAndKeys:@"更多",@"title",@"",@"norImage",@"",@"secImage" ,nil];

 

    [_buttonDatas addObject:dic1];

    [_buttonDatas addObject:dic2];

    [_buttonDatas addObject:dic3];

//    [_buttonDatas addObject:dic4];

//    [_buttonDatas addObject:dic5];

 

}

 

- (void)initTabBar

{

    UIView *tabBar = [[UIView alloc]initWithFrame:CGRectMake(0, HEIGHT_DEVICE - 49, WIDTH_DEVICE, 49)];

    tabBar.backgroundColor = RGBA_COLOR(0, 0, 0, 0.4);

    [self.view addSubview:tabBar];

    UIImageView *backView = [[UIImageView alloc]initWithImage:[UIImage imageNamed:@""]];

    [tabBar addSubview:backView];

    self.backImageView = backView;

    

    //为button添加数据

    _buttonArray = [NSMutableArray array];

    int i = 0;

    for (NSDictionary *dict in self.buttonDatas) {

        LeeButton *btn = [[LeeButton alloc]initWithTitle:[dict objectForKey:@"title"] AndImage:[UIImage imageNamed:[dict objectForKey:@"norImage"]] AndSelectImage:[UIImage imageNamed:[dict objectForKey:@"selImage"]] AndFrame:CGRectMake(i * WIDTH_DEVICE/3, 0, WIDTH_DEVICE/3, 49)];

        btn.tag = 100 + i;

        [btn addTarget:self action:@selector(buttonTouch:) forControlEvents:UIControlEventTouchUpInside];

        [tabBar addSubview:btn];

        [_buttonArray addObject:btn];

        if (i == 0) {

            //backView.center = btn.center;

            //self.selectedIndex = 0;

            btn.selected = YES;

        }

        i ++;

    }

    

}

 

- (void)buttonTouch:(LeeButton *)button

{

    //self.backImageView.center = button.center;

    button.selected = YES;

    for (LeeButton *Btn in self.buttonArray) {

        if (Btn.tag == button.tag && Btn.tag != 0) {

            Btn.selected = YES;

        }else{

            Btn.selected = NO;

        }

    }

    self.selectedIndex = button.tag - 100;

}

 

- (void)addChildControllers

{

    oneViewController *oneVC = [[oneViewController alloc]init];

    oneVC.view.backgroundColor = [UIColor redColor];

    //给每一个控制器包装一个导航栏

    LeeNavigationController *NV1 = [[LeeNavigationController alloc]initWithRootViewController:oneVC];

    oneVC.title = @"电影";

    

    twoViewController *twoVC = [[twoViewController alloc]init];

    twoVC.view.backgroundColor = [UIColor greenColor];

    //给每一个控制器包装一个导航栏

    LeeNavigationController *NV2 = [[LeeNavigationController alloc]initWithRootViewController:twoVC];

    twoVC.title = @"影院";

 

    threeViewController *threeVC = [[threeViewController alloc]init];

    threeVC.view.backgroundColor = [UIColor blueColor];

    //给每一个控制器包装一个导航栏

    LeeNavigationController *NV3 = [[LeeNavigationController alloc]initWithRootViewController:threeVC];

    threeVC.title = @"新闻";

 

    self.viewControllers = @[NV1,NV2,NV3];

    

 

 

}

@end

 

@interface LeeButton : UIButton

 

- (instancetype)initWithTitle:(NSString *)title AndImage:(UIImage *)image AndSelectImage:(UIImage *)selectImage AndFrame:(CGRect)frame;

@end

 

#import "LeeButton.h"

@interface LeeButton()

@property (nonatomic, weak)UIImageView *leeView;

@property (nonatomic, weak)UILabel     *leeLabel;

@property (nonatomic, strong)UIImage   *norImg;

@property (nonatomic, strong)UIImage   *selImg;

@end

@implementation LeeButton

 

- (instancetype)initWithTitle:(NSString *)title AndImage:(UIImage *)image AndSelectImage:(UIImage *)selectImage AndFrame:(CGRect)frame

{

    self = [super initWithFrame:frame];

    if (self) {

        CGFloat width = self.frame.size.width;

        CGFloat height = self.frame.size.height;

        CGFloat imageWH = 30;

        //添加imageView

        UIImageView *imageView = [[UIImageView alloc]initWithFrame:CGRectMake((width - imageWH)*0.5, 5, imageWH, imageWH)];

        imageView.backgroundColor = [UIColor clearColor];

        imageView.contentMode = UIViewContentModeScaleAspectFit;

        imageView.image = image;

        [self addSubview:imageView];

        self.leeView = imageView;

        

        //添加Label

        UILabel *label = [[UILabel alloc]initWithFrame:CGRectMake(0, CGRectGetMaxY(imageView.frame), width, height - CGRectGetMaxY(imageView.frame))];

        label.backgroundColor = [UIColor clearColor];

        label.textAlignment = NSTextAlignmentCenter;

        label.font = [UIFont systemFontOfSize:10];

        label.textColor = [UIColor whiteColor];

        label.text = title;

        [self addSubview:label];

        self.leeLabel = label;

        

        self.norImg = image;

        self.selImg = selectImage;

    }

    return self;

}

- (void)setSelected:(BOOL)selected

{

    [super setSelected:selected];

    NSLog(@"111");

    if (selected) {

        self.leeView.image = self.selImg;

    }else{

        self.leeView.image = self.norImg;

    }

}

@end

posted on 2018-10-30 16:12  Truce_Lee  阅读(151)  评论(0编辑  收藏  举报