中間凸出(百思tabbar上面修改) 按鈕範圍,nav改色。

//
//  WMTabBarController.m
//  百思不得姐
//
//  Created by 王蒙 on 15/7/22.
//  Copyright (c) 2015年 wm. All rights reserved.
//

#import "WMTabBarController.h"
#import "FirstViewController.h"
#import "SecondViewController.h"
#import "WMTabBar.h"
#import "RootViewController.h"
#import "ShopHomeViewController.h"
#import "CRMViewController.h"
#import "WMNavigationController.h"
#import "JHBViewController.h"
#import "OrderListViewController.h"
@interface WMTabBarController ()<UITabBarDelegate>
{
   JHBViewController* jhb;
}
@end

@implementation WMTabBarController


+ (void)initialize{

    NSMutableDictionary *arrText = [NSMutableDictionary dictionary];
    arrText[NSFontAttributeName] = [UIFont systemFontOfSize:12];
    arrText[NSForegroundColorAttributeName] = [UIColor grayColor];
    NSMutableDictionary *selText = [NSMutableDictionary dictionary];
    selText[NSFontAttributeName] = [UIFont systemFontOfSize:12];
    selText[NSForegroundColorAttributeName] = [UIColor blackColor];
    UITabBarItem *item = [UITabBarItem appearance];
    [item setTitleTextAttributes:arrText forState:UIControlStateNormal];
    [item setTitleTextAttributes:selText forState:UIControlStateSelected];
    
}
- (void)viewDidLoad {
    [super viewDidLoad];
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(pushCenter) name:@"pushCenter" object:nil];
    [self setUpVCTitle];
    
}

#pragma mark -建立vc控制器 tabbr替換
- (void)setUpVCTitle{

    [self setUpChildVC:[[FirstViewController alloc] init] image:[UIImage imageNamed:@"firstpage"] selImage:[UIImage imageNamed:@"firstpage_on"] title:@"首頁"];
    
    [self setUpChildVC:[[ShopHomeViewController alloc] init] image:[UIImage imageNamed:@"dianpu"] selImage:[UIImage imageNamed:@"dianpu_on"] title:@"店鋪"];
    
    [self setUpChildVC:[[CRMViewController alloc] init] image:[UIImage imageNamed:@"crm"] selImage:[UIImage imageNamed:@"crm_on"] title:@"CRM"];
  
     jhb = [[JHBViewController alloc] init];
    [self setUpChildVC:jhb image:[UIImage imageNamed:@"jinhuo"] selImage:[UIImage imageNamed:@"jinhuo_on"] title:@"進貨"];
    
    //替換tabBar
    [self setValue:[[WMTabBar alloc] init] forKeyPath:@"tabBar"];

}

#pragma mark - 點擊中間按鈕時通知收到消息調用此方法
- (void)pushCenter{

    if ([[UIApplication sharedApplication].keyWindow.rootViewController isKindOfClass:[WMTabBarController class]]) {
        RootViewController *vc = [[RootViewController alloc] init];
        [vc setHidesBottomBarWhenPushed:YES];
        [self.selectedViewController pushViewController:vc animated:YES];
    }
    NSLog(@"push2");

}
#pragma mark - 設置切換控制器
- (void)setUpChildVC:(UIViewController*)vc image:(UIImage*)image selImage:(UIImage*)selImage title:(NSString*)title{
   
    vc.tabBarItem.title = title;
    [vc.tabBarItem setTitleTextAttributes:[NSDictionary dictionaryWithObjectsAndKeys:[UIColor colorWithRed:0.35 green:0.76 blue:0.96 alpha:1.000],NSForegroundColorAttributeName, nil] forState:UIControlStateSelected];
    [vc.tabBarItem setTitleTextAttributes:[NSDictionary dictionaryWithObjectsAndKeys:[UIColor blackColor],NSForegroundColorAttributeName, nil] forState:UIControlStateNormal];
    vc.tabBarItem.image = [image imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
    vc.tabBarItem.selectedImage = [selImage imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
    
    UINavigationController *nvc = [[UINavigationController alloc] initWithRootViewController:vc];
    [nvc.navigationBar setTitleTextAttributes:@{
                                                 NSForegroundColorAttributeName:[UIColor whiteColor]
                                                 }];
    nvc.navigationBar.tintColor = [UIColor whiteColor];
    [nvc.navigationBar setBackgroundImage:[UIImage new] forBarMetrics:UIBarMetricsDefault];
    UIView *view = [[UIView alloc] initWithFrame:CGRectMake(0, -20, kScreenWidth, 64)];
    view.userInteractionEnabled = NO;
    view.backgroundColor = kbluemainColor;
    [nvc.navigationBar insertSubview:view atIndex:0];
    [self addChildViewController:nvc];

}


#pragma mark - UITabBarDelegate 點擊tabbar上面的四個按鈕
-(void)tabBar:(UITabBar *)tabBar didSelectItem:(UITabBarItem *)Item{
    
    if ([Item.title isEqualToString:@"進貨"]) {

[[NSUserDefaults standardUserDefaults] setObject:@"jhb" forKey:@"thename"]; [[NSUserDefaults standardUserDefaults] synchronize]; self.tabBar.hidden = YES; [jhb loadView]; }else{ self.tabBar.hidden = NO; } } -(void)showTabBar { if (self.tabBar.hidden) { self.tabBar.hidden = NO; } } -(void)hideTabBar { if (!self.tabBar.hidden) { self.tabBar.hidden = YES; } } @end
#import "WMTabBar.h"
#import "UIImage+Color.h"

@interface WMTabBar()
/**按鈕*/
{
    JHBViewController *jhb;
    NSInteger btnheight;
}
@property (nonatomic,weak) UIButton *pushBtn;

@end
@implementation WMTabBar

- (instancetype)initWithFrame:(CGRect)frame{

    self = [super initWithFrame:frame];
    if (self) {
        btnheight = 80;
        [self  setShadowImage:[UIImage imageWithColor:[UIColor clearColor]]];
        self.backgroundImage =[UIImage imageWithColor:[UIColor clearColor]];//去除黑線
        UIView *line = [[UIView alloc]initWithFrame:CGRectMake(0, 0, self.bounds.size.width, 1)];//把黑線從新加上,這樣黑線能夠在按鈕下面了
        line.backgroundColor = [UIColor grayColor];
        [self addSubview:line];
        
        UIButton *btn = [UIButton buttonWithType:UIButtonTypeCustom];
        [btn setTitle:@"掃一掃" forState:UIControlStateNormal];
        [btn setImage:[UIImage imageNamed:@"saoma"] forState:UIControlStateNormal];
        [btn setImage:[UIImage imageNamed:@"saoma"] forState:UIControlStateHighlighted];
        [btn setTitleColor: [UIColor blackColor] forState:UIControlStateNormal];
        [btn setTitleColor: kbluemainColor forState:UIControlStateHighlighted];
        [btn addTarget:self action:@selector(pushlishVC) forControlEvents:UIControlEventTouchUpInside];
        btn.titleLabel.font = [UIFont systemFontOfSize:10.f];

        if (iPhone4){
            [btn setImageEdgeInsets:UIEdgeInsetsMake(0, 8, 0, 0)];
            [btn setTitleEdgeInsets:UIEdgeInsetsMake(0, -[UIScreen mainScreen].bounds.size.width/5+14, -btnheight+15, 0)];
        }else if (iPhone5) {
            [btn setImageEdgeInsets:UIEdgeInsetsMake(0, 8, 0, 0)];
            [btn setTitleEdgeInsets:UIEdgeInsetsMake(0, -[UIScreen mainScreen].bounds.size.width/5+20, -btnheight+15, 0)];
        }else if (iPhone6){
            [btn setImageEdgeInsets:UIEdgeInsetsMake(3, 12, 0, 0)];
            [btn setTitleEdgeInsets:UIEdgeInsetsMake(0, -[UIScreen mainScreen].bounds.size.width/5+30, -btnheight+15, 0)];
        }else if (iPhone6Plus){
            [btn setImageEdgeInsets:UIEdgeInsetsMake(0, 11, 0, 0)];
            [btn setTitleEdgeInsets:UIEdgeInsetsMake(0, -[UIScreen mainScreen].bounds.size.width/5+42, -btnheight+15, 0)];
        }
        
        [self addSubview:btn];
        self.pushBtn = btn;
        [self setBackgroundImage:[UIImage imageNamed:@"tabbar-light"]];

    }
    return  self;
}

#pragma mark -  重寫hitTest方法,去監聽發佈按鈕的點擊,目的是爲了讓凸出的部分點擊也有反應
- (UIView *)hitTest:(CGPoint)point withEvent:(UIEvent *)event {
    
    //這一個判斷是關鍵,不判斷的話push到其餘頁面,點擊發布按鈕的位置也是會有反應的,這樣就很差了
    //self.isHidden == NO 說明當前頁面是有tabbar的,那麼確定是在導航控制器的根控制器頁面
    //在導航控制器根控制器頁面,那麼咱們就須要判斷手指點擊的位置是否在發佈按鈕身上
    //是的話讓發佈按鈕本身處理點擊事件,不是的話讓系統去處理點擊事件就能夠了
    if (self.isHidden == NO) {
        
        //將當前tabbar的觸摸點轉換座標系,轉換到發佈按鈕的身上,生成一個新的點
        CGPoint newP = [self convertPoint:point toView:self.pushBtn];
        
        //判斷若是這個新的點是在發佈按鈕身上,那麼處理點擊事件最合適的view就是發佈按鈕
        if ( [self.pushBtn pointInside:newP withEvent:event]) {
            return self.pushBtn;
        }else{//若是點不在發佈按鈕身上,直接讓系統處理就能夠了
            return [super hitTest:point withEvent:event];
        }
    }
    else {//tabbar隱藏了,那麼說明已經push到其餘的頁面了,這個時候仍是讓系統去判斷最合適的view處理就行了
        return [super hitTest:point withEvent:event];
    }
}


#pragma mark - 點擊中間凸出按鈕
- (void)pushlishVC{
    
    [[NSNotificationCenter defaultCenter] postNotificationName:@"pushCenter" object:nil userInfo: nil];
    
}

#pragma mark - 佈局
- (void)layoutSubviews{

    [super layoutSubviews];
    
    static BOOL isClick = NO;
    //對按鈕進行佈局
    CGSize tabBarWH = self.frame.size;
    self.pushBtn.height = 80;
    self.pushBtn.width = [UIScreen mainScreen].bounds.size.width/5;
    self.pushBtn.center = CGPointMake(tabBarWH.width*0.5, tabBarWH.height*0);
    CGFloat buttonY = 0;
    CGFloat buttonW = tabBarWH.width / 5;
    CGFloat buttonH = tabBarWH.height;
    NSInteger index = 0;
    //對子控件進行佈局
    for (UIControl *btn in self.subviews) {
        if (![btn isKindOfClass:NSClassFromString(@"UITabBarButton")])continue;
        CGFloat buttonX = buttonW * ((index > 1)?(index + 1):index);
        btn.frame = CGRectMake(buttonX, buttonY, buttonW, buttonH);
        
        index++;
        
        if (isClick == NO) {
            
            [btn addTarget:self action:@selector(onClick) forControlEvents:UIControlEventTouchUpInside];
        }
    }
    isClick = YES;

}

//發送通知
- (void)onClick{
   
    NSLog(@"onClick");

}

座標可能有別人寫好的分類。不用判斷一個個調,暫時沒有找到。app

相關文章
相關標籤/搜索