简书 http://www.jianshu.com/p/a64323524e64
系统tabbar和自定义tabbar同时使用,再自定义的子类tabbar中重新约束位置
//
// MainTabBarViewController.m
// YunMaiDriver
//
// Created by zpf on 2017/9/13.
// Copyright © 2017年 YunMai. All rights reserved.
//
#import "MainTabBarViewController.h"
#import "HomePageViewController.h"
#import "DriverTaskListViewController.h"
#import "MessageViewController.h"
#import "MineViewController.h"
#import "CustomTabBar.h"
#import "TripViewController.h"
@interface MainTabBarViewController ()
@property (nonatomic, strong) CustomTabBar *customTabBar;
@end
@implementation MainTabBarViewController
+ (void)initialize {
UITabBarItem *tabBarItem = [UITabBarItem appearance];
NSMutableDictionary *norDict = @{}.mutableCopy;
norDict[NSFontAttributeName] = [UIFont systemFontOfSize:12];
norDict[NSForegroundColorAttributeName] = greyFont_Color;
[tabBarItem setTitleTextAttributes:norDict forState:UIControlStateNormal];
NSMutableDictionary *selDict = @{}.mutableCopy;
selDict[NSFontAttributeName] = norDict[NSFontAttributeName];
selDict[NSForegroundColorAttributeName] = Theme_Color;
[tabBarItem setTitleTextAttributes:selDict forState:UIControlStateSelected];
}
- (void)viewWillAppear:(BOOL)animated
{
[super viewWillAppear:animated];
self.navigationController.navigationBarHidden = YES;
}
- (void)viewDidLoad {
[super viewDidLoad];
CGFloat barItemTitlePositionVertical = 0;
HomePageViewController *ymDriverHomeVC = [[HomePageViewController alloc] init];
UINavigationController *homeNavi = [[UINavigationController alloc] initWithRootViewController:ymDriverHomeVC];
UITabBarItem *homeBarItem = [[UITabBarItem alloc] initWithTitle:@"首页" image:IMAGE(@"首页-未选中") tag:0];
homeBarItem.accessibilityIdentifier = @"tabBar_homePage";
[homeBarItem setTitlePositionAdjustment:UIOffsetMake(0, barItemTitlePositionVertical)];
homeBarItem.selectedImage = [IMAGE(@"首页-选中") imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
homeNavi.tabBarItem = homeBarItem;
DriverTaskListViewController *driverTaskListVC = [[DriverTaskListViewController alloc] init];
UINavigationController *driverTaskNavi = [[UINavigationController alloc] initWithRootViewController:driverTaskListVC];
UITabBarItem *driverTaskBarItem = [[UITabBarItem alloc] initWithTitle:@"任务" image:IMAGE(@"任务-未选中") tag:0];
driverTaskBarItem.accessibilityIdentifier = @"tabBar_driverTask";
[driverTaskBarItem setTitlePositionAdjustment:UIOffsetMake(0, barItemTitlePositionVertical)];
driverTaskBarItem.selectedImage = [IMAGE(@"任务-选中") imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
driverTaskNavi.tabBarItem = driverTaskBarItem;
MessageViewController *messageVC = [[MessageViewController alloc] init];
UINavigationController *messageNavi = [[UINavigationController alloc] initWithRootViewController:messageVC];
UITabBarItem *messageBarItem = [[UITabBarItem alloc] initWithTitle:@"消息" image:IMAGE(@"消息-未选中") tag:0];
messageBarItem.accessibilityIdentifier = @"tabBar_message";
[messageBarItem setTitlePositionAdjustment:UIOffsetMake(0, barItemTitlePositionVertical)];
messageBarItem.selectedImage = [IMAGE(@"消息-选中") imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
messageNavi.tabBarItem = messageBarItem;
MineViewController *myVC = [[MineViewController alloc] init];
UINavigationController *myNavi = [[UINavigationController alloc] initWithRootViewController:myVC];
UITabBarItem *myBarItem = [[UITabBarItem alloc] initWithTitle:@"我的" image:IMAGE(@"我的-未选中") tag:0];
myBarItem.accessibilityIdentifier = @"tabBar_my";
[myBarItem setTitlePositionAdjustment:UIOffsetMake(0, barItemTitlePositionVertical)];
myBarItem.selectedImage = [IMAGE(@"我的-选中") imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
myNavi.tabBarItem = myBarItem;
__weak MainTabBarViewController * weakSelf = self;
_customTabBar = [[CustomTabBar alloc] initWithFrame:CGRectMake(0, 0, iPhoneWidth, 49)];
_customTabBar.tripBtnBlock = ^(UIButton *button) {
NSLog(@"跳转到tripVC");
TripViewController * tripVC = [[TripViewController alloc] init];
[weakSelf.navigationController pushViewController:tripVC animated:YES];
};
_customTabBar.roadBtnBlock = ^(UIButton *sender) {
NSLog(@"跳转到roadVC");
};
[self setValue:_customTabBar forKey:@"tabBar"];
self.viewControllers = @[homeNavi, driverTaskNavi, messageNavi,
//
// CustomTabBar.m
// YunMaiDriver
//
// Created by zpf on 2017/9/13.
// Copyright © 2017年 YunMai. All rights reserved.
//
#import "CustomTabBar.h"
#import "TripAndRoadView.h"
#import "TripViewController.h"
@interface CustomTabBar ()
@property (nonatomic, strong) TripAndRoadView * tarView;
@end
@implementation CustomTabBar
- (instancetype)initWithFrame:(CGRect)frame {
if (self = [super initWithFrame:frame]) {
[self makeUI];
}
return self;
}
- (void)makeUI {
[self removeAllSubviews];
CGFloat itemWith = iPhoneWidth / 5;
_contentBtn = [UIButton buttonWithType:UIButtonTypeCustom];
[_contentBtn setImage:IMAGE(@"中心圈圈") forState:UIControlStateNormal];
[_contentBtn setImage:IMAGE(@"关闭圈圈") forState:UIControlStateSelected];
[_contentBtn setImageEdgeInsets:UIEdgeInsetsMake(5, 0, -5, 0)];
_contentBtn.frame = CGRectMake(0, 0, itemWith, self.size.height);
[_contentBtn addTarget:self action:@selector(contentBtnClick:) forControlEvents:UIControlEventTouchUpInside];
_contentBtn.center = CGPointMake(self.size.width / 2, self.size.height / 2);
[self addSubview:_contentBtn];
}
- (TripAndRoadView *)tarView
{
if (!_tarView) {
UIWindow * window = [[[UIApplication sharedApplication] delegate] window];
_tarView = [[TripAndRoadView alloc] initWithFrame:CGRectMake(0, 0, iPhoneWidth, iPhoneHeight - 50)];
[window addSubview:_tarView];
}
return _tarView;
}
- (void)showTripAndRoadView
{
if (_contentBtn.selected) {
self.tarView.hidden = NO;
}else
{
self.tarView.hidden = YES;
}
__weak CustomTabBar * weakSelf = self;
self.tarView.tapBlock = ^(UITapGestureRecognizer *sender) {
weakSelf.contentBtn.selected = NO;
//
[weakSelf contentBtnClick:nil];
};
self.tarView.tripBlock = ^(UIButton *sender) {
weakSelf.contentBtn.selected = NO;
//
[weakSelf contentBtnClick:nil];
if (weakSelf.tripBtnBlock) {
weakSelf.tripBtnBlock(sender);
}
};
self.tarView.roadBlock = ^(UIButton *sender) {
weakSelf.contentBtn.selected = NO;
//
[weakSelf contentBtnClick:nil];
if (weakSelf.roadBtnBlock) {
weakSelf.roadBtnBlock(sender);
}
};
}
- (void)contentBtnClick:(UIButton *)button {
button.selected = !button.selected;
[self showTripAndRoadView];
}
- (void)layoutSubviews {
[super layoutSubviews];
CGFloat itemWith = iPhoneWidth / 5;
NSInteger index = 0;
for (UIView *childView in self.subviews) {
Class class = NSClassFromString(@"UITabBarButton");
if ([childView isKindOfClass:class]) {
childView.frame = CGRectMake(index * itemWith, 0, itemWith, self.size.height);
index++;
if (index == 2) {
index++;
}
}
}
}
@end
myNavi]; self.selectedIndex = 0;}#pragma mark - 警告- (void)didReceiveMemoryWarning { [super didReceiveMemoryWarning];}@end