码迷,mamicode.com
首页 > 其他好文 > 详细

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

时间:2015-08-12 09:01:46      阅读:127      评论:0      收藏:0      [点我收藏+]

标签:

相信现在的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];

}

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

版权声明:本文为博主原创文章,未经博主允许不得转载。

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

标签:

原文地址:http://blog.csdn.net/tubiebutu/article/details/47438623

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!