ios - 自定義TabBar

BaseTabbarController.h

#import <UIKit/UIKit.h>

@interface BaseTabbarController : UITabBarController

@end

BaseTabbarController.m

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

@interface BaseTabbarController ()<SXTTabbarDelegate>

@property (nonatomic, strong) SXTTabbar * sxtTabbar;

@end

@implementation BaseTabbarController

#pragma mark - SXTTabbarDelegate

- (void)tabbar:(SXTTabbar *)tabbar clickIndex:(SXTItemType)idx {
    
    //當(dāng)前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 <UIKit/UIKit.h>

@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 <UIKit/UIKit.h>

@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
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

友情鏈接更多精彩內(nèi)容