美文网首页
TabBarContorller

TabBarContorller

作者: wpf_register | 来源:发表于2016-09-03 01:45 被阅读429次

    常规项目基本上都是一个TabBarController,下面有三四个模块,每个模块都是一个NavigationController。别致一点的会在tabBar中间自定义一个样式,如 “ + ” 号等。
    系统的UITabBarController自然是可以,但还是不尽如人意。所以项目中多是下面两个第三方TabBarController.

    CYLTabBarController

    #import "HomeViewController.h"
    
    #import "FirstViewController.h"
    #import "SecondViewController.h"
    #import "WPNavigationController.h"
    
    @interface HomeViewController ()<UITabBarDelegate>
    
    @end
    
    @implementation HomeViewController
    //初始化
    - (instancetype)init{
        self = [super init];
        if (self) {
            //设置控制器
            [self setupTabBarController];
            //设置tabBar 高度
            self.tabBarHeight = 60;
            self.tabBarController.tabBar.delegate = self;
            self.moreNavigationController.navigationBarHidden = NO;
        }
        return self;
    }
    //设置子视图
    - (void)setupTabBarController{
        
        FirstViewController *firstVC = [[FirstViewController alloc]init];
        firstVC.view.backgroundColor = [UIColor whiteColor];
        WPNavigationController *firstNav = [[WPNavigationController alloc]initWithRootViewController:firstVC];
        SecondViewController *secondVC = [[SecondViewController alloc]init];
        secondVC.view.backgroundColor = [UIColor greenColor];
        WPNavigationController *secondNav = [[WPNavigationController alloc]initWithRootViewController:secondVC];
        
        
        NSDictionary *dic1 = @{
                               CYLTabBarItemTitle:@"首页",
                               CYLTabBarItemImage:@"home_unselected",
                               CYLTabBarItemSelectedImage:@"home_selected",
                               
                               };
        NSDictionary *dic2 = @{
                               CYLTabBarItemTitle:@"我的",
                               CYLTabBarItemImage:@"me_unselected",
                               CYLTabBarItemSelectedImage:@"me_selected",
                               };
        self.tabBarItemsAttributes = @[dic1,dic2];
    
        //最后一步设置viewControllers
        [self setViewControllers:@[firstNav,secondNav]];
        //详细设置tabBar的样式
        [self customizeTabBarAppearance:self];
    }
    /**
     *  可以详细设置tabBar的样式内容
     */
    - (void)customizeTabBarAppearance:(CYLTabBarController *)tabBarController {
        
        // 普通状态下的文字属性
        NSMutableDictionary *normalAttrs = [NSMutableDictionary dictionary];
        normalAttrs[NSForegroundColorAttributeName] = HEXCOLOR(0x2c2c2c);
        
        // 选中状态下的文字属性
        NSMutableDictionary *selectedAttrs = [NSMutableDictionary dictionary];
        selectedAttrs[NSForegroundColorAttributeName] = HEXCOLOR(0xd81e06);
        
        // 设置文字属性
        UITabBarItem *tabBarItem = [UITabBarItem appearance];
        [tabBarItem setTitleTextAttributes:normalAttrs forState:UIControlStateNormal];
        [tabBarItem setTitleTextAttributes:selectedAttrs forState:UIControlStateSelected];
        
        // 设置背景图片
        UITabBar *tabBar = [UITabBar appearance];
        [tabBar setBarStyle:UIBarStyleDefault];     //设置tabBar样式
        [tabBar setBarTintColor:[UIColor whiteColor]];//设置tabBar背景色
        // 去除 TabBar 自带的顶部阴影
        [tabBar setShadowImage:[[UIImage alloc] init]];    
        
    }
    
    #pragma mark - Delegate Method
    #pragma mark - UITabBarControllerDelegate Method
    - (void)tabBar:(UITabBar *)tabBar didSelectItem:(UITabBarItem *)item{
        if ([item.title isEqualToString:@"我的"]) {
            NSLog(@"点击了我的");
        }
    }
    
    - (void)viewDidLoad {
        [super viewDidLoad];
        // Do any additional setup after loading the view.
    }
    
    

    CYLTabBarController 的父类是UITabBarController,所以可以正常使用父类的所有属性和方法,另外,它最牛X的地方在于自定义特殊tabBarItem时的低耦合性。
    只要在工程中新建一个继承自CYLPlusButton 的类,并实现相应的代理方法,会自动检测生成。

    #import "CYLPlusButtonSubclass.h"
    #import "CYLTabBarController.h"
    
    @interface CYLPlusButtonSubclass ()< CYLPlusButtonSubclassing >
    {
        CGFloat _buttonImageHeight;
    }
    @end
    
    @implementation CYLPlusButtonSubclass
    
    #pragma mark - 注册
    + (void)load {
        [super registerPlusButton];
    }
    
    #pragma mark - 初始化
    - (instancetype)initWithFrame:(CGRect)frame {
        if (self = [super initWithFrame:frame]) {
            
            self.titleLabel.textAlignment = NSTextAlignmentCenter;
            self.adjustsImageWhenHighlighted = NO;
        }
        return self;
    }
    
    #pragma mark上下结构的 button
    - (void)layoutSubviews {//initWithFrame时会触发
        
        [super layoutSubviews];
        
        // 控件大小,间距大小.
        //设置图片大小 Width = Height
        CGFloat const imageViewEdgeWidth   = self.bounds.size.width * 0.6;
        CGFloat const imageViewEdgeHeight  = imageViewEdgeWidth ;
        
        CGFloat const centerOfView    = self.bounds.size.width * 0.5; //X轴中心点
        
        CGFloat const labelLineHeight = self.titleLabel.font.lineHeight; //行高
        
        // imageView 和 titleLabel 中心的 Y 值
        CGFloat const centerOfImageView  =  imageViewEdgeWidth * 0.5;
        CGFloat const centerOfTitleLabel = imageViewEdgeHeight  + labelLineHeight * 0.5 + 5  ;
        
        //imageView position 位置
        self.imageView.bounds = CGRectMake(0, 0, imageViewEdgeWidth, imageViewEdgeHeight);
        self.imageView.center = CGPointMake(centerOfView, centerOfImageView);
        //title position 位置
        self.titleLabel.bounds = CGRectMake(0, 0, self.bounds.size.width, labelLineHeight);
        self.titleLabel.center = CGPointMake(centerOfView, centerOfTitleLabel);
    }
    
    #pragma mark - CYLPlusButtonSubclassing Methods
    
    //奇数时可以不用实现
    + (NSUInteger)indexOfPlusButtonInTabBar {
        return 1;
    }
    /**
     *  带标题
     */
    + (id)plusButton {
        
     CYLPlusButtonSubclass *button = [[CYLPlusButtonSubclass alloc] init];
        UIImage *buttonImage_unSelected = [UIImage imageNamed:@"center_unselected"];
        UIImage *buttonImage_selected = [UIImage imageNamed:@"center"];
        [button setImage:buttonImage_selected forState:UIControlStateSelected];
        [button setImage:buttonImage_unSelected forState:UIControlStateNormal];
        [button setTitle:@"思考" forState:UIControlStateNormal];
        [button setTitleColor:HEXCOLOR(0x2c2c2c) forState:UIControlStateNormal];
        [button setTitleColor:HEXCOLOR(0xd81e06) forState:UIControlStateSelected];
        
        button.titleLabel.font = [UIFont systemFontOfSize:11];
        [button sizeToFit];
       
        //[button addTarget:button action:@selector(clickPublisha) forControlEvents:UIControlEventTouchUpInside];
        return button;
    }
    /**
     *  两个可选的代理方法都在设置button的中心点Y轴方向上的位置
     *  PlusButtonCenterY = multiplierOfTabBarHeight * taBarHeight + constantOfPlusButtonCenterYOffset;
     */
    
    /**
     *  返回值大于0.5表示偏下
     *  返回值小于0.5表示偏上
     *  在图片大小超出tabBar高度时实现这个方法,可以精细控制
     */
    + (CGFloat)multiplierOfTabBarHeight:(CGFloat)tabBarHeight {
        return  0.6;
    }
    /**
     *  返回值大于0会向下偏移
     *  返回值小于0会向上偏移
     */
    + (CGFloat)constantOfPlusButtonCenterYOffsetForTabBarHeight:(CGFloat)tabBarHeight {
        return  -20;
    }
    
    /*
     *  不带标题
     */
    //+ (id)plusButton
    //{
    //
    //    UIImage *buttonImage = [UIImage imageNamed:@"hood.png"];
    //    UIImage *highlightImage = [UIImage imageNamed:@"hood-selected.png"];
    //
    //    CYLPlusButtonSubclass* button = [CYLPlusButtonSubclass buttonWithType:UIButtonTypeCustom];
    //
    //    button.autoresizingMask = UIViewAutoresizingFlexibleRightMargin | UIViewAutoresizingFlexibleLeftMargin | UIViewAutoresizingFlexibleBottomMargin | UIViewAutoresizingFlexibleTopMargin;
    //    button.frame = CGRectMake(0.0, 0.0, buttonImage.size.width, buttonImage.size.height);
    //    [button setBackgroundImage:buttonImage forState:UIControlStateNormal];
    //    [button setBackgroundImage:highlightImage forState:UIControlStateHighlighted];
    //    [button addTarget:button action:@selector(clickPublish) forControlEvents:UIControlEventTouchUpInside];
    //
    //    return button;
    //}
    

    实现下面这个方法后,自定义的item 会跟其它item一样点击后进入新的VC,如果不实现相当于一个特殊的button,并且一定要实现indexOfPlusButtonInTabBar。

    + (UIViewController *)plusChildViewController {
        UIViewController *plusChildViewController = [[UIViewController alloc] init];
        plusChildViewController.view.backgroundColor = [UIColor redColor];
        plusChildViewController.navigationItem.title = @"PlusChildViewController";
        UIViewController *plusChildNavigationController = [[UINavigationController alloc]
                                                       initWithRootViewController:plusChildViewController];
        return plusChildNavigationController;
    }
    - (void)clickPublisha{
        NSLog(@"gggg");
    }
    

    RDVTabBarController

    RDVTabBarController 的父类是UIViewController,所以无法直接使用UITabBarController的方法,图标也无法很方便的自定义。

    @interface MainTabViewController()<RDVTabBarControllerDelegate>
    @end
    
    @implementation SMTMainTabViewController
    
    - (void)viewDidLoad
    {
        [super viewDidLoad];
        self.delegate = self;
        [self setUpControllers];
    }
    -(void)setUpControllers
    {
        FirstViewController *firstVC = [[FirstViewController alloc]init];
        firstVC.view.backgroundColor = [UIColor whiteColor];
        WPNavigationController *firstNav = [[WPNavigationController alloc]initWithRootViewController:firstVC];
        SecondViewController *secondVC = [[SecondViewController alloc]init];
        secondVC.view.backgroundColor = [UIColor greenColor];
        WPNavigationController *secondNav = [[WPNavigationController alloc]initWithRootViewController:secondVC];
    
        
        [self setViewControllers:@[firstNav, secondNav]];
        
        //定制tabbar
        [self customizeTabBar];
    }
    - (void)customizeTabBar
    {
        UIImage *imgSelected = nil;
        UIImage *imgUnselected = nil;
        NSString*title = @"";
        
        NSInteger index = 0;
        for (RDVTabBarItem *item in [[self tabBar] items]){
            if (index == 0){
                imgSelected = [UIImage imageNamed:@"首页"];
                imgUnselected = [UIImage imageNamed:@"un首页"];
                title = @"首页";
            } else if(index == 1){
                imgSelected = [UIImage imageNamed:@"我的"];
                imgUnselected = [UIImage imageNamed:@"un我的"];
                title = @"我的";
            }
            item.title = title;
    
            //调整item 图标和标题的位置,使之平排
           //item.titlePositionAdjustment = UIOffsetMake(20, -11);
           //item.imagePositionAdjustment = UIOffsetMake(0, 3);
            
            [item setFinishedSelectedImage:imgSelected withFinishedUnselectedImage:imgUnselected];
            index++;
        }
    }    
    #pragma mark - RDVTabBarControllerDelegate Methods
    - (BOOL)tabBarController:(RDVTabBarController *)tabBarController shouldSelectViewController:(UIViewController *)viewController{
        return YES;
    }
    

    PS:VC中要得到RDVTabBarController :
    self.rdv_tabBarItem;
    self.rdv_tabBarController

    相关文章

      网友评论

          本文标题:TabBarContorller

          本文链接:https://www.haomeiwen.com/subject/jbekettx.html