IOS-UITabBarController

49 篇文章 0 订阅
3 篇文章 0 订阅
 
 简书 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


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值