1:创建继承于UITabBar的类MyTabBar
2:设置代理实现UITabBar的代理协议
@protocol MyTabBarDelegate
- (void)tabBarDidClickCenterButton:(XPFTabBar *)tabBar;
@end
声明代理
@property (nonatomic, weak) id delegate;
3:在.m初始化方法重写
- (nonnull instancetype)initWithFrame:(CGRect)frame {
if (self = [super initWithFrame:frame]) {
UIView * backView = [[UIView alloc]init];
backView.backgroundColor = [UIColor whiteColor];
backView.layer.cornerRadius = 32.5;
backView.layer.masksToBounds = YES;
[self addSubview:backView];
[backView mas_makeConstraints:^(MASConstraintMaker *make) {
make.centerX.equalTo(self.mas_centerX);
make.top.equalTo(@0).offset(-15);
make.width.equalTo(@65);
make.height.equalTo(@65);
}];
UIButton *publishButton = [[UIButton alloc] init];
[publishButton setBackgroundImage:[UIImage imageNamed:@"WALLET"] forState:UIControlStateNormal];
[publishButton setBackgroundImage:[UIImage imageNamed:@"WALLET"] forState:UIControlStateHighlighted];
publishButton.layer.cornerRadius = 29;
publishButton.layer.masksToBounds = YES;
[backView addSubview:publishButton];
[publishButton mas_makeConstraints:^(MASConstraintMaker *make) {
make.centerX.equalTo(backView.mas_centerX);
make.centerY.equalTo(backView.mas_centerY);
make.width.equalTo(@58);
make.height.equalTo(@58);
}];
[publishButton addTarget:self action:@selector(publishClick) forControlEvents:UIControlEventTouchUpInside];
}
return self;
}
- (void)publishClick {
[self.delegate tabBarDidClickCenterButton:self];
}
//重新设置位置
- (void)layoutSubviews
{
[super layoutSubviews];
// 原来的4个
CGFloat width = self.xpf_width / 5;
int index = 0;
for (UIControl *control in self.subviews) {
if (![control isKindOfClass:[UIControl class]] || [control isKindOfClass:[UIButton class]]) continue;
control.xpf_width = width;
control.xpf_x = index > 1 ? width * (index + 1) : width * index;
if (index == 1) {
}
index++;
}
}
4:创建继承于UITabBarController的MyTabBarController
//重写initialze,定义TabBar
+ (void)initialize {
UITabBarItem *appearance = [UITabBarItem appearance];
NSMutableDictionary *attrs = [NSMutableDictionary dictionary];
attrs[NSForegroundColorAttributeName] = [UIColor darkGrayColor];
[appearance setTitleTextAttributes:attrs forState:UIControlStateSelected];
// [[UITabBar appearance] setBackgroundImage:[UIImage imageNamed:@"BG_image"]]; //tabbar-light
[[UITabBar appearance] setBackgroundColor:[UIColor whiteColor]];
}
- (void)viewDidLoad {
// 替换tabbar
MyTabBar *tabBar = [[MyTabBar alloc] init];
tabBar.adDelegate = self;
[self setValue:tabBar forKeyPath:@"tabBar"];
// 添加
[self setupChildViewControllers];
//去掉顶部的线的效果,即阴影图片和背景图片一样时
CGRect rect = CGRectMake(0, 0, MAINSCREEN.size.width, 1);
UIGraphicsBeginImageContext(rect.size);
CGContextRef context = UIGraphicsGetCurrentContext();
CGContextSetFillColorWithColor(context, [[UIColor clearColor] CGColor]);
CGContextFillRect(context, rect);
UIImage *img = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
[self.tabBar setBackgroundImage:img];
[self.tabBar setShadowImage:img];
}
- (void)setupChildViewControllers {
[self addAppearance];
}
#pragma mark - <添加一个子控制器>
- (void)setUpOneChildViewController:(UIViewController *)vc title:(NSString *)title image:(NSString *)image selImage:(NSString *)selImage {
vc.navigationItem.title = title;
vc.tabBarItem.title = title;
vc.tabBarItem.image = [UIImage imageNamed:image];
vc.tabBarItem.selectedImage = [[UIImage imageNamed:selImage] imageWithRenderingMode:(UIImageRenderingModeAlwaysOriginal)];
[self addChildViewController:[[NavigationController alloc] initWithRootViewController:vc]];
}
#pragma mark - 设置颜色
- (void)addAppearance {
[UINavigationBar appearance];
NSMutableDictionary *attrs = [NSMutableDictionary dictionary];
attrs[NSFontAttributeName] = [UIFont systemFontOfSize:10];
// 未选中的颜色
attrs[NSForegroundColorAttributeName] = [UIColor colorWithRed:138/255.0 green:138/255.0 blue:138/255.0 alpha:1.0];
NSMutableDictionary *selectedAttrs = [NSMutableDictionary dictionary];
selectedAttrs[NSFontAttributeName] = attrs[NSFontAttributeName];
// 点击后的颜色
selectedAttrs[NSForegroundColorAttributeName] = [UIColorChange colorwithHexString:MAINCOLOR];
UITabBarItem *item = [UITabBarItem appearance];
[item setTitleTextAttributes:attrs forState:UIControlStateNormal];
[item setTitleTextAttributes:selectedAttrs forState:UIControlStateSelected];
}
#pragma mark - TabBarDelegate点击中间按钮触发的方法
- (void)tabBarDidClickCenterButton:(XPFTabBar *)tabBar {
self.wvc = [[WalletViewController alloc]init];
self.wvc.view.backgroundColor = [UIColor colorWithWhite:0 alpha:0.5];
[self.wvc.closeButton addTarget:self action:@selector(closeClick:) forControlEvents:UIControlEventTouchUpInside];
[[UIApplication sharedApplication].keyWindow.rootViewController.view addSubview:self.wvc.view];
}
- (void)closeClick:(UIButton *)sender{
[self.wvc.view removeFromSuperview];
self.wvc = nil;
}
// 手势点击事件
- (void)touch {
[self remove_adVeiw];
}
/** 删除 view */
- (void)remove_adVeiw {
[adView removeFromSuperview];
}