iOS 自定义TabBar(仿新浪微博TabBar)
2016-03-31 18:12
615 查看
UITabBar 是一个比较常用的工具
有的时候系统的样式不能满足需求,我们可以考虑自定义
![](https://img-blog.csdn.net/20160331175748007?watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQv/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70/gravity/Center)
中间的+ 就属于自定义样式
我们可以通过自定义TabBar来实现
首先新建一个项目
创建一个类继承字UITabBar
![](https://img-blog.csdn.net/20160331180400447?watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQv/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70/gravity/Center)
创建一个协议和按钮
在MyTabBar 的实现中首先添加按钮 设置按钮的图片 点击等
将按钮点击的方法设置到他的代理中
同时 ,重新调整按钮的位置
好了 自定义TabBar 好了
我们在创建一个类MainViewController 继承自UITabBarController
使用KVC方法设置TabBar
同时实现代理方法
好了 剩下的就是设置UITabBarControl
我们在项目默认生成的AppDelegate 中设置rootViewController为我们的MainViewControler
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
// Override point for customization after application launch.
//创建几个ViewController
FirstViewController *firstVC = [[FirstViewController alloc]init];
SecondViewController *secondVC = [[SecondViewController alloc]init];
ThirdViewController *thirdVC = [[ThirdViewController alloc]init];
ForthViewController *forthVC = [[ForthViewController alloc]init];
//创建MainViewController并将创建好的ViewController添加到TabBar上
MainViewController *tabBarC=[[MainViewController alloc]init];
tabBarC.viewControllers=@[firstVC,secondVC,thirdVC,forthVC];
for (UIViewController *controller in tabBarC.viewControllers) {
UIViewController *view= controller.view;
}
self.window.rootViewController=tabBarC;
return YES;
}
完成之后看下效果
![](https://img-blog.csdn.net/20160401123929209?watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQv/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70/gravity/Center)
TabBar的样式已经出来了 如果需要,可以自己换成微博的图片就可以了
源代码 我上传到QQ群 大家有兴趣去看下
demo:【60331MYTabBar.zip】
苹果开发群 :414319235 欢迎加入,共同学习
有的时候系统的样式不能满足需求,我们可以考虑自定义
中间的+ 就属于自定义样式
我们可以通过自定义TabBar来实现
首先新建一个项目
创建一个类继承字UITabBar
创建一个协议和按钮
#import <UIKit/UIKit.h> @class MYTabBar; @protocol MYTabBarDelegate <UITabBarDelegate> @optional - (void)tabBarDidClickPlusButton:(MYTabBar *)tabBar; @end @interface MYTabBar : UITabBar @property (strong,nonatomic) UIButton *plusBtn; @property (nonatomic, weak) id<MYTabBarDelegate> tabBarDelegate; @end
在MyTabBar 的实现中首先添加按钮 设置按钮的图片 点击等
- (id)initWithFrame:(CGRect)frame { self = [super initWithFrame:frame]; if (self) { // 添加一个按钮到tabbar中 UIButton *plusBtn = [[UIButton alloc] init]; [plusBtn setImage:[UIImage imageNamed:@"tabbar_mainbtn1.png"] forState:UIControlStateNormal]; CGRect temp = plusBtn.frame; temp.size=plusBtn.currentImage.size; plusBtn.frame=temp; [plusBtn addTarget:self action:@selector(plusClick) forControlEvents:UIControlEventTouchUpInside]; [self addSubview:plusBtn]; self.plusBtn = plusBtn; } return self; }
将按钮点击的方法设置到他的代理中
#pragma mark 加号按钮点击事件处理器 - (void)plusClick { // 通知代理 if ([self.tabBarDelegate respondsToSelector:@selector(tabBarDidClickPlusButton:)]) { [self.tabBarDelegate tabBarDidClickPlusButton:self]; } }
同时 ,重新调整按钮的位置
- (void)layoutSubviews { [super layoutSubviews]; // 1.设置加号按钮的位置 CGPoint temp = self.plusBtn.center; temp.x=self.frame.size.width/2; temp.y=self.frame.size.height/2; self.plusBtn.center=temp; // 2.设置其它UITabBarButton的位置和尺寸 CGFloat tabbarButtonW = self.frame.size.width / 5; CGFloat tabbarButtonIndex = 0; for (UIView *child in self.subviews) { Class class = NSClassFromString(@"UITabBarButton"); if ([child isKindOfClass:class]) { // 设置宽度 CGRect temp1=child.frame; temp1.size.width=tabbarButtonW; temp1.origin.x=tabbarButtonIndex * tabbarButtonW; child.frame=temp1; // 增加索引 tabbarButtonIndex++; if (tabbarButtonIndex == 2) { tabbarButtonIndex++; } } } }
好了 自定义TabBar 好了
我们在创建一个类MainViewController 继承自UITabBarController
使用KVC方法设置TabBar
- (void)viewDidLoad { [super viewDidLoad]; // Do any additional setup after loading the view. MYTabBar *tabBar = [[MYTabBar alloc] init]; tabBar.tabBarDelegate = self; [self setValue:tabBar forKeyPath:@"tabBar"]; }
同时实现代理方法
#pragma mark - MYTabBarDelegate代理方法 - (void)tabBarDidClickPlusButton:(MYTabBar *)tabBar { AddViewController *addVC= [[AddViewController alloc] init]; [self presentViewController:addVC animated:YES completion:nil]; }
好了 剩下的就是设置UITabBarControl
我们在项目默认生成的AppDelegate 中设置rootViewController为我们的MainViewControler
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
// Override point for customization after application launch.
//创建几个ViewController
FirstViewController *firstVC = [[FirstViewController alloc]init];
SecondViewController *secondVC = [[SecondViewController alloc]init];
ThirdViewController *thirdVC = [[ThirdViewController alloc]init];
ForthViewController *forthVC = [[ForthViewController alloc]init];
//创建MainViewController并将创建好的ViewController添加到TabBar上
MainViewController *tabBarC=[[MainViewController alloc]init];
tabBarC.viewControllers=@[firstVC,secondVC,thirdVC,forthVC];
for (UIViewController *controller in tabBarC.viewControllers) {
UIViewController *view= controller.view;
}
self.window.rootViewController=tabBarC;
return YES;
}
完成之后看下效果
TabBar的样式已经出来了 如果需要,可以自己换成微博的图片就可以了
源代码 我上传到QQ群 大家有兴趣去看下
demo:【60331MYTabBar.zip】
苹果开发群 :414319235 欢迎加入,共同学习
相关文章推荐
- 峰回路转,Firefox 浏览器即将重返 iOS 平台
- 峰回路转,Firefox 浏览器即将重返 iOS 平台
- 不可修补的 iOS 漏洞可能导致 iPhone 4s 到 iPhone X 永久越狱
- iOS 12.4 系统遭黑客破解,漏洞危及数百万用户
- 每日安全资讯:NSO,一家专业入侵 iPhone 的神秘公司
- [转][源代码]Comex公布JailbreakMe 3.0源代码
- 简单几行JS Code实现IE邮件转发新浪微博
- js实现新浪微博首页效果
- jquery实现仿新浪微博评论滚动效果
- jQuery实现仿新浪微博浮动的消息提示框(可智能定位)
- 新浪微博API开发简介之用户授权(PHP基础篇)
- 讲解iOS开发中基本的定位功能实现
- iOS中定位当前位置坐标及转换为火星坐标的方法
- js判断客户端是iOS还是Android等移动终端的方法
- iOS应用中UISearchDisplayController搜索效果的用法
- IOS开发环境windows化攻略
- iOS应用中UITableView左滑自定义选项及批量删除的实现
- 浅析iOS应用开发中线程间的通信与线程安全问题
- 检测iOS设备是否越狱的方法