相信现在的APP10个里面有九个是有Tabbar的,但是很多人甚是很多公司都在用系统自带的tabbar。当然这也不是不可以,而且项目中就那几行代码,效果又一样。但是,别忘了还有一个但是。然并卵,这样并不符合苹果的设计理念。
好了 老规矩话不多说,先上图:
这个是高仿美团的tabbar。
接下来上主要代码吧:
`-(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];
}
`
`
@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;`
`
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;
}
}`
老实说简书的markdown我还不是很熟悉,代码看起来有点费劲。你看起来有什么问题可以提出来。我们一起探讨。源码就不上传了。待下一个版本比较完善了 一并上传。
2015 - 08 - 11
23 :31 祝好
网友评论