无聊的时候,就抠抠脚,无聊的时候,吃吃手,无聊的时候,自定义一个TabBar,苹果的东西很神奇,KVO和KVC都是很 diao 的机制,有些系统的东西拿不到就用KVC,然后就能实现很多功能,拿到私有的方法,但是这里不对KVC介绍,只是使用KVC来拿系统的控件,实现自定义的一些效果;
少说废话上代码:
1.首先创建一个AirTabBar.h继承于UITabBar
AirTabBar.h
#import <UIKit/UIKit.h>
@class AirTabBar;
@protocol AirTabBarDelegate <UITabBarDelegate>
@optional
//创建代理用来触发点击事件
- (void)tabBarDidClickPlusButton:(AirTabBar *)tabBar;
@end
@interface AirTabBar : UITabBar
@property (nonatomic,weak) id<AirTabBarDelegate> delegate;
@end
AirTabBar.m
#import "AirTabBar.h"
#import "UIView+Extension.h"
@interface AirTabBar()
@property (nonatomic, weak) UIButton *plusBtn;
@end
@implementation AirTabBar
- (id)initWithFrame:(CGRect)frame
{
self = [super initWithFrame:frame];
if (self) {
// 添加一个按钮到tabbar中
UIButton *plusBtn = [[UIButton alloc] init];
[plusBtn setBackgroundImage:[UIImage imageNamed:@"tabbar_compose_button"] forState:UIControlStateNormal];
[plusBtn setBackgroundImage:[UIImage imageNamed:@"tabbar_compose_button_highlighted"] forState:UIControlStateHighlighted];
[plusBtn setImage:[UIImage imageNamed:@"tabbar_compose_icon_add"] forState:UIControlStateNormal];
[plusBtn setImage:[UIImage imageNamed:@"tabbar_compose_icon_add_highlighted"] forState:UIControlStateHighlighted];
plusBtn.size = plusBtn.currentBackgroundImage.size;
[plusBtn addTarget:self action:@selector(plusClick) forControlEvents:UIControlEventTouchUpInside];
[self addSubview:plusBtn];
self.plusBtn = plusBtn;
}
return self;
}
/**
* 加号按钮点击
*/
- (void)plusClick
{
// 通知代理
if ([self.delegate respondsToSelector:@selector(tabBarDidClickPlusButton:)]) {
[self.delegate tabBarDidClickPlusButton:self];
}
}
- (void)layoutSubviews
{
#warning [super layoutSubviews] 一定要调用
[super layoutSubviews];
// 1.设置加号按钮的位置
self.plusBtn.centerX = self.width * 0.5;
self.plusBtn.centerY = self.height * 0.5;
// 2.设置其他tabbarButton的位置和尺寸
CGFloat tabbarButtonW = self.width / 5;
CGFloat tabbarButtonIndex = 0;
for (UIView *child in self.subviews) {
Class class = NSClassFromString(@"UITabBarButton");
if ([child isKindOfClass:class]) {
// 设置宽度
child.width = tabbarButtonW;
// 设置x
child.x = tabbarButtonIndex * tabbarButtonW;
// 增加索引
tabbarButtonIndex++;
if (tabbarButtonIndex == 2) {
tabbarButtonIndex++;
}
}
}
}
@end
接下来回忆一下 iOS 封装UITabBarController (一)得代码,在 AirTabBariewController.m直接插入自定义的tabBard
使用方法:
AirTabBariewController.m
#import "AirTabBariewController.h"
#import "AirComposeViewController.h"
#import "AirTabBar.h"
@interface AirTabBariewController ()<AirTabBarDelegate>
@end
@implementation AirTabBariewController
- (void)viewDidLoad {
[super viewDidLoad];
AirHomeViewController *home = [[AirHomeViewController alloc] init];
[self addChildVc:home title:@"首页" image:@"tabbar_home" selectedImage:@"tabbar_home_selected"];
AirMessageCenterViewController *messageCenter = [[AirMessageCenterViewController alloc] init];
[self addChildVc:messageCenter title:@"消息" image:@"tabbar_message_center" selectedImage:@"tabbar_message_center_selected"];
AirDiscoverViewController *discover = [[AirDiscoverViewController alloc] init];
[self addChildVc:discover title:@"发现" image:@"tabbar_discover" selectedImage:@"tabbar_discover_selected"];
AirProfileViewController *profile = [[AirProfileViewController alloc] init];
[self addChildVc:profile title:@"我" image:@"tabbar_profile" selectedImage:@"tabbar_profiler_selected"];
//更改系统自带的tabbar
AirTabBar *tabbar = [[AirTabBar alloc] init];
[self setValue:tabbar forKey:@"tabBar"];
}
/**
* 封装添加子控制器的方法
*
* @param childVc 控制器
* @param title 标题
* @param image 正常状态下的的图标
* @param selectedImage 选中状态下的图标
*/
- (void)addChildVc:(UIViewController *)childVc title:(NSString *)title image:(NSString *)image selectedImage:(NSString *)selectedImage
{
//设置标题
childVc.tabBarItem.title = title;
childVc.tabBarItem.image = [UIImage imageNamed:image];
//需要设置照片的模式,用照片原图,默认是蓝色的
childVc.tabBarItem.selectedImage = [[UIImage imageNamed:selectedImage] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
//创建修改字体颜色的字典,同时可以设置字体的内边距;
NSMutableDictionary *textAttrs = [NSMutableDictionary dictionary];
textAttrs[NSForegroundColorAttributeName] = [UIColor colorWithRed:123/255 green:123/255 blue:123/255 alpha:1];
NSMutableDictionary *selectedTextAttrs = [NSMutableDictionary dictionary];
selectedTextAttrs[NSForegroundColorAttributeName] = [UIColor orangeColor];
[childVc.tabBarItem setTitleTextAttributes:textAttrs forState:UIControlStateNormal];
[childVc.tabBarItem setTitleTextAttributes:selectedTextAttrs forState:UIControlStateSelected];
childVc.view.backgroundColor = [UIColor yellowColor];
//不要忘记添加到父控制器上
[self addChildViewController:childVc];
}
#pragma mark - AirTabBarDelegate代理方法
- (void)tabBarDidClickPlusButton:(AirTabBar *)tabBar
{
AirComposeViewController *compose = [[AirComposeViewController alloc] init];
[self presentViewController:compose animated:YES completion:nil];
}
@end
效果图如下:(PS:自定义点击事件)
如果转载请注明转于:AirZilong的博客