RDVTabBarController的使用介绍

使用

建议直接CocoaPods管理,对CocoaPods有兴趣的童鞋可以戳cocoapods-install-usage

结构

import "RDVTabBarItem.h"

#import "RDVViewController.h"
#import "ViewController.h"
#import "RDVTabBarItem.h"

@interface RDVViewController ()

@end

@implementation RDVViewController

- (void)viewDidLoad {
     [super viewDidLoad];
     
    MainViewController *mainView = [[MainViewController alloc] init];
    UINavigationController *NAV2 = [[UINavigationController alloc] initWithRootViewController:mainView];
    
    SettingViewController *setView = [[SettingViewController alloc] init];
    UINavigationController *NAV3 = [[UINavigationController alloc] initWithRootViewController:setView];
    
//    UIStoryboard *storyboard = [UIStoryboard storyboardWithName:@"Main" bundle:nil];
//    UIViewController *infoView = [storyboard instantiateViewControllerWithIdentifier:@"InformationViewController"];
    InformationViewController *infoView = [[InformationViewController alloc] init];
    UINavigationController *NAV1 = [[UINavigationController alloc] initWithRootViewController:infoView];
    
    [self setViewControllers:@[NAV1,NAV2,NAV3]];
    
    [self customizeTabBarForController];
    self.selectedIndex = 1;
    self.delegate = self;

 }
 

自定义 

#define kSafeArea_Bottom (kDevice_Is_iPhoneX? 34: 0)
#define ScreenWidth   [[UIScreen mainScreen] bounds].size.width
#define ScreenHeight  [[UIScreen mainScreen] bounds].size.height

- (void)customizeTabBarForController{
    NSArray *tabBarItemImages = @[@"A1_50", @"A2_50", @"A3_50"];
    NSArray *tabBarItemSelectImages = @[@"B1_50", @"B2_50", @"B3_50"];
    NSArray *tabBarItemTitles = @[LocalString(@"Information"),LocalString(@"Robot status"),LocalString(@"Setting")];
    NSInteger index = 0;
    for (RDVTabBarItem *item in [[self tabBar] items]) {
        item.tag = 1000 + index;
        item.titlePositionAdjustment = UIOffsetMake(0, 2);
        [item setTitle:[tabBarItemTitles objectAtIndex:index]];
        UIImage *selectedimage = [UIImage imageNamed:[tabBarItemSelectImages objectAtIndex:index]];
        UIImage *unselectedimage = [UIImage imageNamed:[tabBarItemImages objectAtIndex:index]];
        [item setFinishedSelectedImage:selectedimage withFinishedUnselectedImage:unselectedimage];
        index++;
    }

//适配iPhoneX机型
    if (ScreenHeight < 700) {
        [self.tabBar setHeight:49.0 + kSafeArea_Bottom];
    }else{
        [self.tabBar setHeight:60.0 + kSafeArea_Bottom];
    }
    [self.tabBar setContentEdgeInsets:UIEdgeInsetsMake(kSafeArea_Bottom / 2, 0, 0, 0)];
    self.tabBar.translucent = YES;
    //self.tabBar.backgroundView.backgroundColor = kColorNavBG;
    self.tabBar.backgroundView.backgroundColor = [UIColor colorWithRed:245/255.0
                                                            green:245/255.0
                                                             blue:245/255.0
                                                            alpha:0.9];

}

 

下载地址:https://github.com/robbdimitrov/RDVTabBarController

AppDelegate里面的不多说

例如

self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
     
RDVViewController *rootViewController = [[RDVViewController alloc] init];
self.window.rootViewController = rootViewController;
[self.window makeKeyAndVisible];

这样你的tabBar基本搭建好了,但是还需要完善一些,比如,角标设置,push隐藏等。

  • Push隐藏tabBar,你只需要这样即可

  • - (void)viewWillAppear:(BOOL)animated{
        [super viewWillAppear:animated];
        [[self rdv_tabBarController] setTabBarHidden:YES animated:YES];
    }

    设置角标数

  •  [[self rdv_tabBarItem] setBadgeValue:@"6"];

    RDVTabBarControllerDelegate,相信你看就会明白,好的方法命名很重要啊~

  • - (BOOL)tabBarController:(RDVTabBarController *)tabBarController shouldSelectViewController:(UIViewController *)viewController{
        if (viewController.rdv_tabBarItem.tag == 1002) {
            AppDelegate *appDelegate = (AppDelegate *) [[UIApplication sharedApplication] delegate];
            
            if (appDelegate.currentPeripheral == nil && appDelegate.status == 1) {
                [NSObject showHudTipStr:NSLocalizedString(@"Bluetooth not connected", nil)];
                return NO;
            }
        }
        return YES;
    }
    
    - (void)tabBarController:(RDVTabBarController *)tabBarController didSelectViewController:(UIViewController *)viewController
    {
        if ([viewController isKindOfClass:[UINavigationController class]]) {
            
            [(UINavigationController *)viewController popToRootViewControllerAnimated:YES];
            
        }
    }

    美中不足的地方,还请大家不吝赐教。

  • 第三方库汇总: https://blog.csdn.net/C_philadd/article/details/83750300

     

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值