1:建立繼承於UITabBar的類MyTabBarapp
2:設置代理實現UITabBar的代理協議ide
@protocol MyTabBarDelegate <UITabBarDelegate>atom
- (void)tabBarDidClickCenterButton:(XPFTabBar *)tabBar;.net
@end代理
聲明代理orm
@property (nonatomic, weak) id<XPFTabBarDelegate> delegate;繼承
3:在.m初始化方法重寫事件
- (nonnull instancetype)initWithFrame:(CGRect)frame {圖片
if (self = [super initWithFrame:frame]) {rem
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];
}