• 还在用系统自带的?那你那就OUT了!


    相信如今的APP10个里面有九个是有Tabbar的,可是非常多人甚是非常多公司都在用系统自带的tabbar。当然这也不是不能够,并且项目中就那几行代码。效果又一样。

    可是,别忘了另一个可是。然并卵。这样并不符合苹果的设计理念。
    好了 老规矩话不多说,先上图:
    这里写图片描写叙述
    这个是高仿美团的tabbar。
    接下来上主要代码吧:
    自己定义tabbar.h

    @class JFTabBar;
    
    //给每一个按钮定义协议 与 方法
    @protocol tabbarDelegate <NSObject>
    @optional
    -(void)tabBar:(JFTabBar * )tabBar didselectedButtonFrom:(int)from to:(int)to;
    @end
    
    @interface JFTabBar : UIView
    @property (weak ,nonatomic)JFTabBarButton *selectedButton;
    /**
     *  给自己定义的tabbar加入按钮
     */
    -(void)addTabBarButtonWithItem:(UITabBarItem *)itme;
    @property(nonatomic , weak) id <tabbarDelegate> delegate;
    
    

    自己定义tabbar.m

    #import "JFTabBar.h"
    #import "JFTabBarButton.h"
    
    @implementation JFTabBar
    
    -(void)addTabBarButtonWithItem:(UITabBarItem *)itme{
        //1.创建按钮
        JFTabBarButton *button = [[JFTabBarButton alloc]init];
        [self addSubview:button];
        /*
         [button setTitle:itme.title forState:UIControlStateNormal];
         [button setImage:itme.image forState:UIControlStateNormal];
         [button setImage:itme.selectedImage forState:UIControlStateSelected];
         [button setBackgroundImage:[UIImage imageWithName:@"tabbar_slider"] forState:UIControlStateSelected];
         */
        //设置数据
        button.item = itme;
    
        //监听点击button
        [button addTarget:self  action:@selector(buttonClick:) forControlEvents:UIControlEventTouchDown];
    
        //默认选中
        if (self.subviews.count == 1) {
            [self buttonClick:button];
        }
    
    
    }
    
    /**
     * button监听事件
     *
     */
    -(void)buttonClick:(JFTabBarButton*)button{
    
        if ([self.delegate respondsToSelector:@selector(tabBar:didselectedButtonFrom:to:)]
            )
        {
            [self.delegate tabBar:self didselectedButtonFrom:(int)self.selectedButton.tag to:(int)button.tag];
        }
        self.selectedButton.selected = NO;
        button.selected = YES;
        self.selectedButton = button;
    
    }
    
    -(void)layoutSubviews{
        [super layoutSubviews];
    
        CGFloat buttonW = self.frame.size.width/ self.subviews.count ;
        CGFloat buttonH = self.frame.size.height;
        CGFloat buttonY = 0 ;
    
        for ( int index = 0; index < self.subviews.count; index++) {
            //1.取出按钮
            JFTabBarButton *button = self.subviews[index];
    
            //2. 设置按钮的frame
    
            CGFloat buttonX = index * buttonW;
    
            button.frame = CGRectMake(buttonX, buttonY, buttonW, buttonH) ;
    
            //绑定tag;
            button.tag = index;
        }
    }
    

    继承tabbatControler.m

    #import "JFTabBarViewController.h"
    #import "JFTabBar.h"
    #import "ViewController.h"
    #import "JFMineViewController.h"
    #import "JFMoreViewController.h"
    #import "JFMerchantViewController.h"
    #import "JFVisitViewController.h"
    
    @interface JFTabBarViewController ()<tabbarDelegate>
    
    @property(nonatomic ,strong)JFTabBar *costomTabBar;
    
    @end
    
    @implementation JFTabBarViewController
    
    - (void)viewDidLoad {
        [super viewDidLoad];
    
        //初始化tabbar
        [self setUpTabBar];
    
        //加入子控制器
        [self setUpAllChildViewController];
    }
    
    //取出系统自带的tabbar并把里面的按钮删除掉
    -(void)viewWillAppear:(BOOL)animated{
        [super viewWillAppear:YES];
        for ( UIView * child in  self.tabBar.subviews) {
    
            if ([child isKindOfClass:[UIControl class]]) {
                [child removeFromSuperview];
            }
        }
    }
    
    -(void)setUpTabBar{
        JFTabBar *customTabBar = [[JFTabBar alloc]init];
        customTabBar.delegate = self;
    //    customTabBar.backgroundColor = [UIColor redColor];
        customTabBar.frame = self.tabBar.bounds;
        self.costomTabBar = customTabBar;
        [self.tabBar addSubview:customTabBar];
    
    }
    -(void)tabBar:(JFTabBar *)tabBar didselectedButtonFrom:(int)from to:(int)to{
        NSLog(@"%d, %d", from, to);
        self.selectedIndex = to;
        NSLog(@"%lu", (unsigned long)self.selectedIndex);
    
    }
    
    -(void)setUpAllChildViewController{
        ViewController *homeVC = [[ViewController alloc]init];
        [self setupChildViewController:homeVC title:@"首页" imageName:@"icon_tabbar_homepage" seleceImageName:@"icon_tabbar_homepage_selected"];
    
        JFVisitViewController *visitVC = [[JFVisitViewController alloc]init];
        [self setupChildViewController:visitVC title:@"上门" imageName:@"icon_tabbar_onsite" seleceImageName:@"icon_tabbar_onsite_selected"];
    
        JFMerchantViewController *merchantVC = [[JFMerchantViewController alloc]init];
        [self setupChildViewController:merchantVC title:@"商家" imageName:@"icon_tabbar_merchant_normal" seleceImageName:@"icon_tabbar_merchant_normal_selected"];
    
        JFMineViewController *mineVC = [[JFMineViewController alloc]init];
        [self setupChildViewController:mineVC title:@"我的" imageName:@"icon_tabbar_mine" seleceImageName:@"icon_tabbar_mine_selected"];
    
        JFMoreViewController *moreVC = [[JFMoreViewController alloc]init];
        [self setupChildViewController:moreVC title:@"很多其它" imageName:@"icon_tabbar_misc" seleceImageName:@"icon_tabbar_misc_selected"];
    
    }
    
    -(void)setupChildViewController:(UIViewController*)controller title:(NSString *)title imageName:(NSString *)imageName seleceImageName:(NSString *)selectImageName{
        controller.title = title;
        controller.tabBarItem.image = [UIImage imageNamed:imageName];
        controller.tabBarItem.selectedImage = [UIImage imageNamed:selectImageName];
    
        //包装导航控制器
        UINavigationController *nav = [[UINavigationController alloc]initWithRootViewController:controller];
        [self addChildViewController:nav];
    
        [self.costomTabBar addTabBarButtonWithItem:controller.tabBarItem];
    
    }
    
    

    以上是部分主要代码
    有问题能够提出来。我们一起探讨。
    祝好。

  • 相关阅读:
    python常见排序算法解析
    分析python日志重复输出问题
    Mysql数据库基础
    横屏竖屏
    禁止iOS的弹性滚动 微信的下拉回弹
    移动性能
    取消双击上滑(针对iso)
    关于微信端 顶部会撑开页面的解决方案
    CSS动画简介
    browser-sync 使用简介
  • 原文地址:https://www.cnblogs.com/slgkaifa/p/7256360.html
Copyright © 2020-2023  润新知