ios - 自定义TabBar

BaseTabbarController.h

#import 

@interface BaseTabbarController : UITabBarController

@end

BaseTabbarController.m

#import "BaseTabbarController.h"
#import "BaseNavViewController.h"
#import "SXTTabbar/SXTTabbar.h"

@interface BaseTabbarController ()

@property (nonatomic, strong) SXTTabbar * sxtTabbar;

@end

@implementation BaseTabbarController

#pragma mark - SXTTabbarDelegate

- (void)tabbar:(SXTTabbar *)tabbar clickIndex:(SXTItemType)idx {
    
    //当前tabbar的索引
    self.selectedIndex = idx;
}

#pragma mark - getters and setters

- (SXTTabbar *)sxtTabbar {
    
    if (!_sxtTabbar) {
        _sxtTabbar = [[SXTTabbar alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, 49)];
        _sxtTabbar.backgroundColor = [UIColor blackColor];
        _sxtTabbar.delegate = self;
    }
    return _sxtTabbar;
}


#pragma mark - life cycle

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
    //加载所有视图控制器
    [self configViewControllers];
    
    //加载自定义tabbar
    [self.tabBar addSubview:self.sxtTabbar];

}

#pragma mark - privte methods

- (void)configViewControllers {
    
    NSMutableArray * viewControlNames = [NSMutableArray arrayWithArray:@[@"First",@"Second",@"Third",@"Fourth"]];
    
    NSString * path = [[NSBundle mainBundle] pathForResource:@"TabbarList.plist" ofType:nil];
    NSArray * contentList = [NSArray arrayWithContentsOfFile:path];
    NSArray * titles = [contentList valueForKey:@"title"];
    
    for (NSUInteger i = 0; i < viewControlNames.count; i ++) {
        
        NSString * controllerName = [viewControlNames[i] stringByAppendingString:@"ViewController"];
        
        UIViewController * vc = [[NSClassFromString(controllerName) alloc] init];
        
        vc.title = titles[i];
        
        BaseNavViewController * nav = [[BaseNavViewController alloc] initWithRootViewController:vc];
        
        [viewControlNames replaceObjectAtIndex:i withObject:nav];
    }
    
    self.viewControllers = viewControlNames;
    
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

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

BaseNavViewController.h

#import 

@interface BaseNavViewController : UINavigationController

@end

BaseNavViewController.m

#import "BaseNavViewController.h"

@interface BaseNavViewController ()

@end

@implementation BaseNavViewController

+ (void)initialize {
    
    UINavigationBar * bar = [UINavigationBar appearance];
    
    bar.barTintColor = [UIColor blackColor];
    bar.barStyle = UIBarStyleBlack;
    
    [bar setTitleTextAttributes:@{NSForegroundColorAttributeName:[UIColor whiteColor]}];
}

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

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

BaseViewController.m

#import "BaseViewController.h"
#import "UIColor+Random.h"

@interface BaseViewController ()

@end

@implementation BaseViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.view.backgroundColor = [UIColor randomColor];
}

FirstViewController.m

#import "FirstViewController.h"
#import "ViewController.h"

@interface FirstViewController ()

@end

@implementation FirstViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
    self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithTitle:@"下一页" style:UIBarButtonItemStylePlain target:self action:@selector(nextAction)];
}

- (void)nextAction {
    
    ViewController * vc = [[ViewController alloc] init];
    vc.hidesBottomBarWhenPushed = YES;
    [self.navigationController pushViewController:vc animated:YES];
}
#import 

@interface UIColor (Random)

+ (UIColor *)randomColor;

@end
#import "UIColor+Random.h"

@implementation UIColor (Random)

+ (UIColor *)randomColor {
    
    CGFloat red = (arc4random() % 255)/ 255.0;
    CGFloat green = arc4random_uniform(255)/255.0;
    CGFloat blue = arc4random_uniform(255)/255.0;
    
    return [UIColor colorWithRed:red green:green blue:blue alpha:1];
}

@end

你可能感兴趣的:(ios - 自定义TabBar)