猫猫学iOS 之微博项目实战(4)微博自定义tabBar中间的添加按钮
来源:互联网 发布:大众软件 pdf 编辑:程序博客网 时间:2024/05/17 05:09
猫猫分享,必须精品
原创文章,欢迎转载。转载请注明:翟乃玉的博客
地址:http://blog.csdn.net/u013357243
一:效果图
自定义tabBar实现最下面中间的添加按钮
二:思路
首先在自己的tabBarController中把系统的tabBar设置成自己的tabBar(NYTabBar),这里由于tabBar的属性是readonly的,所以我们要用kvc或者是消息管理来设置他
然后就写自己的NYTabBar。这个写起来首先 遍历当前tabBar上的所有view,如果是UITabBarButton,就取出来重新设置他们的位置,并且重新赋值,接下来就是空出中间的添加的位置,然后把添加按钮放上去,就ok了。
三:代码
调用
首先是如何调用的代码
//自定义tabBar NYTabBar *tabBar = [[NYTabBar alloc]initWithFrame:self.tabBar.frame]; //用kvc把readly的tabBar属性改成自定义的 [self setValue:tabBar forKey:@"tabBar"];
NYTabBar.m
NYTabBar.h文件就不写了 ,里面啥都没有
NYTabBar.m:
//// NYTabBar.m// 猫猫微博//// Created by apple on 15-7-24.// Copyright (c) 2015年 znycat. All rights reserved.//#import "NYTabBar.h"@interface NYTabBar()/** * 添加增加按钮 */@property (nonatomic, weak) UIButton *addButton;@end@implementation NYTabBar-(UIButton *)addButton{ if (_addButton == nil) { UIButton *btn = [UIButton buttonWithType:UIButtonTypeCustom]; [btn setImage:[UIImage imageNamed:@"tabbar_compose_icon_add"] forState:UIControlStateNormal]; [btn setBackgroundImage:[UIImage imageNamed:@"tabbar_compose_button"] forState:UIControlStateNormal]; [btn setImage:[UIImage imageNamed:@"tabbar_compose_icon_add_highlighted"] forState:UIControlStateHighlighted]; [btn setBackgroundImage:[UIImage imageNamed:@"tabbar_compose_button_highlighted"] forState:UIControlStateHighlighted]; _addButton = btn; //是按钮的尺寸默认跟背景图片一样大 [btn sizeToFit]; [self addSubview:_addButton]; } return _addButton;}//调整子空间的位置-(void)layoutSubviews{ [super layoutSubviews]; CGFloat w = self.bounds.size.width; CGFloat h = self.bounds.size.height; CGFloat btnX = 0; CGFloat btnY = 0; CGFloat btnW = w / 5; CGFloat btnH = h; int i = 0; //1 , 遍历当前tabBar上的所有view for (UIView *tabBarBtn in self.subviews) { //2,如果是UITabBarButton,就取出来重新设置他们的位置 if ([tabBarBtn isKindOfClass:NSClassFromString(@"UITabBarButton")]) { btnX = i * btnW; tabBarBtn.frame = CGRectMake(btnX, btnY, btnW, btnH); //当到了第二个时候,再加一个位置空竹添加按钮的位置。 if (i==1) { i++; } i++; } } //设置添加按钮 add按钮的位置 self.addButton.center = CGPointMake(w * 0.5, h * 0.5);}@end
四:补充
调用的时候还可以用消息管理
//自定义tabBar NYTabBar *tabBar = [[NYTabBar alloc]initWithFrame:self.tabBar.frame]; /*这个地方还有一种用消息管理设置的,这样也可以,使用个步骤是 1:先导入头文件#import <objc/message.h> 2:到项目中Build Settings中查找msg ,然后把yes改成no 3:代码写objc_msgSend调用就行了 注意:这样的方法框架中比较多,目的为了不让人看懂,还有显得c牛逼,——————没啥鸟用。。 */ //用消息管理设置 objc_msgSend(self, @selector(setTabBar:),tabBar);
步骤:
1:先导入头文件
#import <objc/message.h>
2:到项目中Build Settings中查找msg ,然后把yes改成no
3:代码写objc_msgSend调用就行了
注意:这样的方法框架中比较多,目的为了不让人看懂,还有显得c牛逼,——————没啥鸟用。。
12 0
- 猫猫学iOS 之微博项目实战(4)微博自定义tabBar中间的添加按钮
- AJ学IOS 之微博项目实战(4)微博自定义tabBar中间的添加按钮
- iOS tabbar中间添加按钮
- iOS tabbar中间添加按钮
- iOS之UI TabBarcontroller----自定义tabBar/改变tabBar中间按钮的样式(一)
- iOS Tabbar中间添加凸起按钮
- iOS 中间按钮突出的tabbar
- iOS Tabbar中间添加凸起可旋转按钮
- iOS 自定义tabbar中间按钮 超简单方法
- 猫猫学iOS 之微博项目实战(1)微博主框架-子控制器的添加
- 猫猫学iOS 之微博项目实战(1)微博主框架-子控制器的添加
- iOS 自定义TabBar(仿新浪微博TabBar)
- iOS开发-新浪微博自定义tabbar的实现
- AJ学IOS 之微博项目实战(1)微博主框架-子控制器的添加
- 猫猫学iOS 之微博项目实战(5)微博自定义搜索框searchBar
- 猫猫学iOS 之微博项目实战(2)微博主框架-自定义导航控制器NavigationController
- 自定义TabBar条,实现重写TabBar的TabBarItem,然后在中间额外加一个按钮
- iOS 自定义 中间带突起圆形的tabbar
- UVALive 6907
- 思维导图之----javascript中function类型知识树
- 2.10-关系运算
- UVA - 11212 Editing a Book
- 单调队列
- 猫猫学iOS 之微博项目实战(4)微博自定义tabBar中间的添加按钮
- iOS中的一些小知识点
- UI____UIEvent以及响应者链
- 美国“懒人经济”风生水起
- NSOperation - 02
- Android 控件开发之Button
- 2.11-逻辑运算
- hadoop深入研究:(二)——java访问hdfs
- NYOJ —水池数目