1:創(chuàng)建繼承于UITabBar的類MyTabBar
2:設(shè)置代理實(shí)現(xiàn)UITabBar的代理協(xié)議
@protocol?MyTabBarDelegate
- (void)tabBarDidClickCenterButton:(XPFTabBar *)tabBar;
聲明代理
@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];
}
//重新設(shè)置位置
- (void)layoutSubviews
{
? ? [super layoutSubviews];
? ? // 原來的4個(gè)
? ? 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:創(chuàng)建繼承于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];
? ? //去掉頂部的線的效果,即陰影圖片和背景圖片一樣時(shí)
? ? 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 - <添加一個(gè)子控制器>
- (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 - 設(shè)置顏色
- (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];
? ? // 點(diǎn)擊后的顏色
? ? selectedAttrs[NSForegroundColorAttributeName] = [UIColorChange colorwithHexString:MAINCOLOR];
? ? UITabBarItem *item = [UITabBarItem appearance];
? ? [item setTitleTextAttributes:attrs forState:UIControlStateNormal];
? ? [item setTitleTextAttributes:selectedAttrs forState:UIControlStateSelected];
}
#pragma mark - TabBarDelegate點(diǎn)擊中間按鈕觸發(fā)的方法
- (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;
}
// 手勢(shì)點(diǎn)擊事件
- (void)touch {
? ? [self remove_adVeiw];
}
/** 刪除 view */
- (void)remove_adVeiw {
? ? [adView removeFromSuperview];
}