RDVTabBarController的使用介绍

段干浩荡
2023-12-01

使用

建议直接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

     

 类似资料: