自定义Tabbar
来源:互联网 发布:mac系统安装软件 编辑:程序博客网 时间:2024/05/21 07:56
//
// QYFTabBar.h
// 自定义Tabbar
//
// Created by ztt on 2017/12/21.
// Copyright © 2017年 中天科技. All rights reserved.
//
#import <UIKit/UIKit.h>
@class QYFTabBar;
@protocol QYFTabBarDelegate<UITabBarDelegate>
@optional
- (void)tabBarDidClickPlusButton:(QYFTabBar *)tabBar;
@end
@interface QYFTabBar : UITabBar
@property(nonatomic,weak)id<QYFTabBarDelegate>delegate;
#import "QYFTabBar.h"
#import "UIView+Frame.h"
#import "XMGConst.h"
@interface QYFTabBar ()
@property (nonatomic,weak) UIButton *plusButton;
/** 上一次点击的按钮 */
@property (nonatomic,weak) UIControl *previousClickedTabBarButton;
@end
@implementation QYFTabBar
@dynamic delegate;
- (UIButton *)plusButton
{
if (_plusButton ==nil) {
UIButton *btn = [UIButtonbuttonWithType:UIButtonTypeCustom];
[btn setImage:[UIImageimageNamed:@"btn_sys_click"]forState:UIControlStateNormal];
[btn setImage:[UIImageimageNamed:@"btn_sys_click"]forState:UIControlStateHighlighted];
[btn sizeToFit];
[self addSubview:btn];
[btn addTarget:selfaction:@selector(plusBtnClick)forControlEvents:UIControlEventTouchUpInside];
_plusButton = btn;
}
return_plusButton;
}
/**
* 加号按钮点击
*/
- (void)plusBtnClick
{
// 通知代理
if ([self.delegaterespondsToSelector:@selector(tabBarDidClickPlusButton:)]) {
[self.delegatetabBarDidClickPlusButton:self];
}
}
- (void)layoutSubviews
{
[superlayoutSubviews];
// 跳转tabBarButton位置
NSInteger count =self.items.count;
CGFloat btnW = self.xmg_width / (count + 1);
CGFloat btnH = self.xmg_height;
CGFloat x = 0;
int i = 0;
// 私有类:打印出来有个类,但是敲出来没有,说明这个类是系统私有类
// 遍历子控件 调整布局
NSLog(@"%lu",(unsignedlong)self.subviews.count);
for (UIControl *tabBarButtonin self.subviews) {
if ([tabBarButton isKindOfClass:NSClassFromString(@"UITabBarButton")]) {
// 设置previousClickedTabBarButton默认值为最前面的按钮
if (i == 0 && self.previousClickedTabBarButton ==nil) {
self.previousClickedTabBarButton = tabBarButton;
}
if (i == 2) {
i += 1;
}
x = i * btnW;
tabBarButton.frame = CGRectMake(x, 0, btnW, btnH);
i++;
// UIControlEventTouchDownRepeat : 在短时间内连续点击按钮
// 监听点击
[tabBarButton addTarget:selfaction:@selector(tabBarButtonClick:)forControlEvents:UIControlEventTouchUpInside];
}
}
// 调整发布按钮位置
self.plusButton.center =CGPointMake(self.xmg_width *0.5, self.xmg_height *0.3);
}
/**
* tabBarButton的点击
*/
- (void)tabBarButtonClick:(UIControl *)tabBarButton
{
if (self.previousClickedTabBarButton == tabBarButton) {
// 发出通知,告知外界tabBarButton被重复点击了
[[NSNotificationCenterdefaultCenter]postNotificationName:XMGTabBarButtonDidRepeatClickNotificationobject:nil];
}
self.previousClickedTabBarButton = tabBarButton;
}
//
// QYFTabBarController.h
// 自定义Tabbar
//
// Created by ztt on 2017/12/21.
// Copyright © 2017年 中天科技. All rights reserved.
//
#import <UIKit/UIKit.h>
@interface QYFTabBarController : UITabBarController
//
// QYFTabBarController.m
// 自定义Tabbar
//
// Created by ztt on 2017/12/21.
// Copyright © 2017年 中天科技. All rights reserved.
//
#import "QYFTabBarController.h"
#import "UIImage+Image.h"
#import "QYFTabBar.h"
@interface QYFTabBarController ()<QYFTabBarDelegate>
@end
@implementation QYFTabBarController
// 只会调用一次
+ (void)load
{
// 获取哪个类中UITabBarItem
UITabBarItem *item = [UITabBarItemappearanceWhenContainedIn:self,nil];
// 设置按钮选中标题的颜色:富文本:描述一个文字颜色,字体,阴影,空心,图文混排
// 创建一个描述文本属性的字典
NSMutableDictionary *attrs = [NSMutableDictionarydictionary];
attrs[NSForegroundColorAttributeName] = [UIColorblackColor];
[item setTitleTextAttributes:attrsforState:UIControlStateSelected];
// 设置字体尺寸:只有设置正常状态下,才会有效果
NSMutableDictionary *attrsNor = [NSMutableDictionarydictionary];
attrsNor[NSFontAttributeName] = [UIFontsystemFontOfSize:13];
[item setTitleTextAttributes:attrsNorforState:UIControlStateNormal];
}
#pragma mark - 生命周期方法
- (void)viewDidLoad {
[superviewDidLoad];
// Do any additional setup after loading the view.
// 1 添加子控制器(5个子控制器) -> 自定义控制器 -> 划分项目文件结构
[selfsetupAllChildViewController];
// 2 设置tabBar上按钮内容 -> 由对应的子控制器的tabBarItem属性
[selfsetupAllTitleButton];
// 3.自定义tabBar
[selfsetupTabBar];
}
#pragma mark - 自定义tabBar
- (void)setupTabBar
{
QYFTabBar *tabBar = [[QYFTabBaralloc] init];
tabBar.delegate = self;
[self setValue:tabBarforKey:@"tabBar"];
}
#pragma mark - 添加所有子控制器
- (void)setupAllChildViewController
{
UIViewController *VC1 = [[UIViewControlleralloc]init];
VC1.view.backgroundColor = [UIColorredColor];
UINavigationController *nav1 = [[UINavigationControlleralloc] initWithRootViewController:VC1];
[selfaddChildViewController:nav1];
UIViewController *VC2 = [[UIViewControlleralloc]init];
VC2.view.backgroundColor = [UIColoryellowColor];
UINavigationController *nav2 = [[UINavigationControlleralloc] initWithRootViewController:VC2];
[selfaddChildViewController:nav2];
UIViewController *VC3 = [[UIViewControlleralloc]init];
VC3.view.backgroundColor = [UIColorgrayColor];
UINavigationController *nav3 = [[UINavigationControlleralloc] initWithRootViewController:VC3];
[selfaddChildViewController:nav3];
UIViewController *VC4 = [[UIViewControlleralloc]init];
VC4.view.backgroundColor = [UIColorblueColor];
UINavigationController *nav4 = [[UINavigationControlleralloc] initWithRootViewController:VC4];
[selfaddChildViewController:nav4];
}
// 设置tabBar上所有按钮内容
- (void)setupAllTitleButton
{
UINavigationController *nav =self.childViewControllers[0];
nav.title = @"tabbar1";
nav.tabBarItem.image = [UIImageimageNamed:@"icon_sy"];
// 快速生成一个没有渲染图片
nav.tabBarItem.selectedImage = [UIImageimageOriginalWithName:@"icon_sy_click"];
UINavigationController *nav1 =self.childViewControllers[1];
nav1.title = @"tabbar2";
nav1.tabBarItem.image = [UIImageimageNamed:@"icon_sz"];
nav1.tabBarItem.selectedImage = [UIImageimageOriginalWithName:@"icon_sz_click"];
UINavigationController *nav2 =self.childViewControllers[2];
nav2.title = @"tabbar3";
nav2.tabBarItem.image = [UIImageimageNamed:@"icon_sy"];
nav2.tabBarItem.selectedImage = [UIImageimageOriginalWithName:@"icon_sz_click"];
UINavigationController *nav3 =self.childViewControllers[3];
nav3.title = @"tabbar4";
nav3.tabBarItem.image = [UIImageimageNamed:@"icon_sz"];
nav3.tabBarItem.selectedImage = [UIImageimageOriginalWithName:@"icon_sy_click"];
}
- 自定义TabBar
- 自定义TabBar
- 自定义Tabbar
- 自定义tabbar
- 自定义tabbar
- 自定义 TabBar
- 自定义TabBar
- 自定义tabBar
- 自定义TabBar
- 自定义TabBar
- 自定义Tabbar
- 自定义tabbar
- 自定义 TabBar
- 自定义TabBar
- 自定义tabBar
- 自定义tabBar
- 自定义tabBar
- 自定义TabBar
- 从0开始学习SpringCould(8)--SpringBoot 分环境读取配置文件
- dubbo服务启动注册报 java.net.UnknownHostException 未知的名称或服务解决办法
- bootstrap treeview 下拉树 (二) checkBox多选
- Android Studio3.0.0之前首次安装通用配置
- Ubuntu下Lenovo笔记本无法使用无线网卡上网的问题
- 自定义Tabbar
- 浅谈 IIC I2C 总线协议
- hdu 4135 Co-prime(容互原理 DFS+位运算求解)
- 梯度下降算法中的Adagrad和Adadelta
- DeepNet深度学习框架的使用
- 数据结构_序列_字符串
- JavaScript~ajax~分页
- 通过pip下载python相关模块的方法
- URL 长链地址转短链 URL地址