码迷,mamicode.com
首页 > 其他好文 > 详细

中间凸出(百思tabbar上面修改) 按钮范围,nav改色。

时间:2016-06-20 23:55:23      阅读:301      评论:0      收藏:0      [点我收藏+]

标签:

//
//  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");

}

坐标可能有别人写好的分类。不用判断一个个调,暂时没有找到。

中间凸出(百思tabbar上面修改) 按钮范围,nav改色。

标签:

原文地址:http://www.cnblogs.com/X-Bin/p/5602147.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!