美文网首页iOS
iOS 自定义tabbar调节中间按钮凸起

iOS 自定义tabbar调节中间按钮凸起

作者: huangxiongbiao | 来源:发表于2017-05-12 15:52 被阅读325次

代码:参考黑马明杰的微博项目中的自定义bar,加入一点自己的思路

一、思路讲解

自定义tabbar替换定义的tabbar,在自定义tabbar内调节相关按钮的位置及样式,调节响应链的点击方法

二、相关问题解决

1、自定义tabbar继承UITabBar在layoutSubviews方法中调节中间按钮的位置,将位置往上调节。
- (void)layoutSubviews
{
#warning [super layoutSubviews] 一定要调用
    [super layoutSubviews];
    // 1.设置加号按钮的位置
//    self.plusBtn.centerX = self.width * 0.5;
//    self.plusBtn.y = -2;
    // 2.设置其他tabbarButton的位置和尺寸
//    CGFloat tabbarButtonW = self.width / 5;
    int tabbarButtonIndex = 0;
    for (UIView *child in self.subviews) {
        Class class = NSClassFromString(@"UITabBarButton");
        if ([child isKindOfClass:class]) {
//            // 设置宽度
//            child.width = tabbarButtonW;
//            // 设置x
//            child.x = tabbarButtonIndex * tabbarButtonW;
//
//            // 增加索引
            if (tabbarButtonIndex == 2) {
//                [self bringSubviewToFront:child];
                child.height = 55;
                child.y = -6;
            }
            tabbarButtonIndex++;
        }
    }
}
2、分隔线解决

但是分隔线会在最上层显示很丑。解决方法将分隔线隐藏,自己写一个分隔线加上去,就不会挡住凸起的部分,记得中间的图标不要透明的

- (id)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
      
        //设置背景,去除tabbar的黑线
        [self setShadowImage:[HXBColor imageFromColor:[UIColor clearColor]]];
        [self setBackgroundImage:[HXBColor imageFromColor:[UIColor whiteColor]]];
        
        UIView *lineView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, kScreenWidth, 1)];
        lineView.backgroundColor = kLineMainColor;
        [self addSubview:lineView];
        self.alpha = 1;
    }
    return self;
}
3、响应链解决

突出部分超出了tabbar的范围点击上方没有效果需要调节响应链方法
根据范围自己设置

-(BOOL)pointInside:(CGPoint)point withEvent:(UIEvent *)event
{
   BOOL result = [super pointInside:point withEvent:event];
    if (result) {
        return result;
    }
    if (self.plusBtn.x<point.x&&(self.plusBtn.x+self.plusBtn.width)>point.x&&self.plusBtn.y<point.y&&(self.plusBtn.y+self.plusBtn.height)>point.y ) {
        return YES;
    }
    return NO;
}
4、使用方法

自定义好tabbar后,替换原来的tabbar

   // 2.更换系统自带的tabbar
    HWTabBar *tabBar = [[HWTabBar alloc] init];
    [self setValue:tabBar forKeyPath:@"tabBar"];

三、示例代码

@interface HXBTabbarControlle ()<UIAlertViewDelegate>

@property(nonatomic,strong)HXBNavigationController *managerNaviVC;

@end

@implementation HXBTabbarControlle

-(UIStatusBarStyle)preferredStatusBarStyle
{
    self.view.backgroundColor = kBackgroundGrayDefautColor;
    return UIStatusBarStyleLightContent;  //默认的值是黑色的
}

+(void)initialize
{
    UITabBarItem *item = [UITabBarItem appearance];
    [item setTitleTextAttributes:@{NSForegroundColorAttributeName:kNavBarMainColor,NSFontAttributeName:[UIFont systemFontOfSize:11]} forState:UIControlStateSelected];
    [item setTitleTextAttributes:@{NSForegroundColorAttributeName:SHColor(109, 109, 109),NSFontAttributeName:[UIFont systemFontOfSize:11]} forState:UIControlStateNormal];
    
    UITabBar *bar = [UITabBar appearance];
    bar.barTintColor = [UIColor whiteColor];
    
//    HWTabBar *bar = [HWTabBar appearance];
//    bar.barTintColor = [UIColor whiteColor];
}

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
//    HomeVC *home = [HomeVC new];
    [self loginVertify];//登录验证
    SearchHotelMainVC *home = [SearchHotelMainVC new];
    [self addChildVc:home title:@"预定" imageName:@"btn_zhu" selectImageName:@"btn_zhu_h"];

    OrderMainVC *order = [OrderMainVC new];
    [self addChildVc:order title:@"订单" imageName:@"btn_dd" selectImageName:@"btn_dd_h"];
    
    OpenDoorCenterVC *door = [OpenDoorCenterVC new];
    [self addChildVc:door title:@"自助入住" imageName:@"tab_km" selectImageName:@"tab_km_h"];
    
    ManagerServeVC *manager = [ManagerServeVC new];
    [self addChildVc:manager title:@"服务" imageName:@"tab_fw" selectImageName:@"tab_fw_h"];
    
    MineVC *mine = [MineVC new];
    [self addChildVc:mine title:@"我的" imageName:@"btn-w" selectImageName:@"btn-w_h"];
    
    // 2.更换系统自带的tabbar
    HWTabBar *tabBar = [[HWTabBar alloc] init];
    [self setValue:tabBar forKeyPath:@"tabBar"];
}


-(void)addChildVc:(UIViewController*)childVC title:(NSString*)title imageName:(NSString*)imageName selectImageName:(NSString*)selectImageName
{
    childVC.title = title;
    childVC.tabBarItem = [[UITabBarItem alloc] initWithTitle:title image:[[UIImage imageNamed:imageName] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal] selectedImage:[[UIImage imageNamed:selectImageName] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal]];
    HXBNavigationController *nav = [[HXBNavigationController alloc] initWithRootViewController:childVC];
    [self addChildViewController:nav];
}

自定义tabbar的代码

#import <UIKit/UIKit.h>

@class HWTabBar;

#warning 因为HWTabBar继承自UITabBar,所以称为HWTabBar的代理,也必须实现UITabBar的代理协议
@protocol HWTabBarDelegate <UITabBarDelegate>
@optional
- (void)tabBarDidClickPlusButton:(UIButton *)plusBtn;
@end

@interface HWTabBar : UITabBar
@property (nonatomic, weak) id<HWTabBarDelegate> delegate;
@end
#import "HWTabBar.h"
#import "HXBColor.h"
#import "TabbarButton.h"

@interface HWTabBar()
@property (nonatomic, weak) UIButton *plusBtn;
@end

@implementation HWTabBar

- (id)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        //设置背景,去除tabbar的黑线
        [self setShadowImage:[HXBColor imageFromColor:[UIColor clearColor]]];
        [self setBackgroundImage:[HXBColor imageFromColor:[UIColor whiteColor]]];
        
        UIView *lineView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, kScreenWidth, 1)];
        lineView.backgroundColor = kLineMainColor;
        [self addSubview:lineView];
        self.alpha = 1;
    }
    return self;
}
//通知方法
-(void)upDatePlusBtn
{
    self.plusBtn.selected = YES;
}

-(void)dealloc
{
    //移除通知
    [[NSNotificationCenter defaultCenter] removeObserver:self name:kNotificationKey object:nil];
}

/**
 *  加号按钮点击
 */
- (void)plusClick
{
    // 通知代理
    if ([self.delegate respondsToSelector:@selector(tabBarDidClickPlusButton:)]) {
        [self.delegate tabBarDidClickPlusButton:self.plusBtn];
    }
}

- (void)layoutSubviews
{
#warning [super layoutSubviews] 一定要调用
    [super layoutSubviews];
    // 1.设置加号按钮的位置
//    self.plusBtn.centerX = self.width * 0.5;
//    self.plusBtn.y = -2;
    // 2.设置其他tabbarButton的位置和尺寸
//    CGFloat tabbarButtonW = self.width / 5;
    int tabbarButtonIndex = 0;
    for (UIView *child in self.subviews) {
        Class class = NSClassFromString(@"UITabBarButton");
        if ([child isKindOfClass:class]) {
//            // 设置宽度
//            child.width = tabbarButtonW;
//            // 设置x
//            child.x = tabbarButtonIndex * tabbarButtonW;
//
//            // 增加索引
            if (tabbarButtonIndex == 2) {
//                [self bringSubviewToFront:child];
                child.height = 55;
                child.y = -6;
            }
            tabbarButtonIndex++;
        }
    }
}

-(BOOL)pointInside:(CGPoint)point withEvent:(UIEvent *)event
{
   BOOL result = [super pointInside:point withEvent:event];
    if (result) {
        return result;
    }
    if (self.plusBtn.x<point.x&&(self.plusBtn.x+self.plusBtn.width)>point.x&&self.plusBtn.y<point.y&&(self.plusBtn.y+self.plusBtn.height)>point.y ) {
        return YES;
    }
    return NO;
}

@end

相关文章

网友评论

本文标题:iOS 自定义tabbar调节中间按钮凸起

本文链接:https://www.haomeiwen.com/subject/uhaztxtx.html