还在用系统自带的?那你那就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];
}
以上是部分主要代码
有问题能够提出来。我们一起探讨。
祝好。
还在用系统自带的?那你那就OUT了!的更多相关文章
- Notepad2替代系统自带的记事本
事情是这样的,平时我经常把一些文字复制到记事本中编辑好了再复制到目标位置,可以在系统自带的记事本中替换删除一些内容,记事本小巧,占用很少的资源,我很喜欢:但今天复制的内容中有很多数字和一些我不想要的内 ...
- Android去掉listView,gridView等系统自带阴影
当我们使用listView的时候,拉到顶,或是拉到底部的时候,我们会发现有系统自带的阴影效果出现,不同手机出现的颜色可能还会不一样. 在以前我始终都有注意到此问题,一直以为是系统自带的,不能去掉.也没 ...
- 利用win7系统自带的dos命令把笔记本无线网卡当无线路由器(无线AP发射器)
利用win7系统自带的dos命令把笔记本无线网卡当无线路由器(无线AP发射器). 1.打开win7开始菜单,找到命令提示符选项,以管理员身份运行cmd.2.在命令行上输入:netsh wlan set ...
- Android manifest之系统自带的permission
Android manifest之系统自带的permission 本文描述Android系统自带的permission.点击查看:“关于permission的原始定义和说明”.点击查看:“Androi ...
- 关于自定义tabBar时修改系统自带tabBarItem属性造成的按钮顺序错乱的问题相关探究
关于自定义tabBar时修改系统自带tabBarItem属性造成的按钮顺序错乱的问题相关探究 测试代码:http://git.oschina.net/Xiyue/TabBarItem_TEST 简 ...
- 系统自带的日志管理工具-rsyslogd
系统自带的日志管理工具-rsyslogd 作者:尹正杰 版权声明:原创作品,谢绝转载!否则将追究法律责任. 一.日志管理简介 1.什么是日志 系统日志是记录系统中硬件.软件和系统问题的信息,同时还可以 ...
- Win10系统自带输入法的人机交互设计
过了寒假回校以后,我的电脑重装了系统,为了提升系统运行的速度,自己装了一个内存条同时对硬盘进行了重新的分区,对电脑内的文件也进行了重新的整理,电脑的运行速度提高了很多.老多同学都说win10系统好用, ...
- 笨重的mfc还在基于系统控件,熟练的mfc工程师还比不过学习Qt一个月的学生开发效率高(比较精彩,韦易笑)
作者:韦易笑链接:https://www.zhihu.com/question/29636221/answer/45102191来源:知乎著作权归作者所有,转载请联系作者获得授权. 更新:擦,本来只有 ...
- 本文介绍使用windows系统自带的远程桌面mstsc连接Centos 7.x远程桌面的基本方法。
本文介绍使用windows系统自带的远程桌面mstsc连接Centos 7.x远程桌面的基本方法. 一.前言 我希望用windows远程访问centos图形界面.xmanager连接centos远程桌 ...
随机推荐
- VS2010安装包制作
最近对软件安装包制作研究了一下,下面记录了一种比较简单,不用写代码的方法. 1.New Project---->Other Project Types ---->Visual Studio ...
- 新疆大学ACM-ICPC程序设计竞赛五月月赛(同步赛)B 杨老师的游戏【暴力/next-permutation函数/dfs】
链接:https://www.nowcoder.com/acm/contest/116/B 来源:牛客网 题目描述 杨老师给同学们玩个游戏,要求使用乘法和减法来表示一个数,他给大家9张卡片,然后报出一 ...
- 线段树【 bzoj3132 】【p4145 】上帝造题的七分钟2 / 花神游历各国
题目大意 给定一个区间 支持开方和查询区间值操作 (多组数据 分析 如果一个区间的最大值小于1,那就没有开方的必要了(具体不会证明,听大佬讲的 一个数经过多次开方就会变成1(可以用计算器试一下 因此我 ...
- 集群/分布式/微服务/SOA 转
https://www.cnblogs.com/Java3y/p/9479410.html 二.集群/分布式/微服务/SOA是什么? 像我这种技术小白,看到这些词(集群/分布式/微服务/SOA)的时候 ...
- Android UI 常用图标尺寸规范
1. 程序启动图标: LDPI (Low Density Screen,120 DPI),其图标大小为 36 x 36 px. MDPI (Medium Density Screen, 160 DPI ...
- ArcMAP定义投影坐标
WGS84形式的高斯克吕格投影在ArcGIS系统中是不存在的,需要自己去定义.下面为MARK的定义过程.
- 【转】Cvmat与IplImage的相互转换
seyvlei 原文地址 1.IplImage转Cvmat IplImage* src = cvLoadImage(); CvMat* mat=cvCreateMat(src->height,s ...
- FragmentTransaction的commit的异步操作
FragmentTransaction是异步的,commit()仅是相当于把操作加入到FragmentManager的队列,然后FragmentManager会在某一个时刻来执行,并不是立即执行.所以 ...
- Tomcat部署多个应用时日志文件路径相同引起的问题
今天给客户部署新系统,一个 前台应用,一个后台应用,由于用的是同一个架子,日志文件路径也没改,所以两个应用的日志文件是同一个文件,这样应用运行时,就会出现只能访问一个应用的情况,如果两个应用同时访问, ...
- OpenGL投影矩阵【转】
OpenGL投影矩阵 概述 透视投影 正交投影 概述 计算机显示器是一个2D平面.OpenGL渲染的3D场景必须以2D图像方式投影到计算机屏幕上.GL_PROJECTION矩阵用于该投影变换.首先,它 ...