IOS开发之——彩票-自定义TabBar(2)

一 概述

TabBar 图片尺寸引起的问题

不超过TabBar尺寸超过TabBar尺寸

二 自定义TabBar

2.1 自定义TabBar

TabBar.h
#import <UIKit/UIKit.h>

//如果需要传参数给其他对象,block才需要定义参数
//typedef void(^ILTabBarBlock)(int selectedIndex);
@class ILTabBar;

@protocol ILTabBarDelegate<NSObject>

@optional
-(void)tabBar:(ILTabBar *)tabBar didSelectedIndex:(int)index;

@end

@interface ILTabBar : UIView
//@property (nonatomic,copy) ILTabBarBlock block;

@property (nonatomic,weak) id<ILTabBarDelegate> delegate;

@end
TabBar.m
#import "ILTabBar.h"
#import "ILTabBarButton.h"

@interface ILTabBar()
@property (nonatomic,weak) UIButton *selectedButton;

@end

@implementation ILTabBar

- (instancetype)initWithFrame:(CGRect)frame
{
    self=[super initWithFrame:frame];
    if(self)
    {
        [self addBtns];
    }
    return self;
}

-(void)addBtns
{
    NSString *imageName=nil;
    NSString *selImageName=nil;
    
    
    for(int i=0;i<5;i++)
    {
        ILTabBarButton *btn=[ILTabBarButton buttonWithType:UIButtonTypeCustom];
        btn.tag=i;
        
        imageName=[NSString stringWithFormat:@"TabBar%d",i+1];
        selImageName=[NSString stringWithFormat:@"TabBar%dSel",i+1];
        //设置按钮的图片
        [btn setBackgroundImage:[UIImage imageNamed:imageName] forState:UIControlStateNormal];
        [btn setBackgroundImage:[UIImage imageNamed:selImageName] forState:UIControlStateSelected];
        
        //监听按钮的点击
        [btn addTarget:self action:@selector(btnClick:) forControlEvents:UIControlEventTouchDown];
        
        [self addSubview:btn];
        
        //选中第一个
        if(i==0)
        {
            [self btnClick:btn];
        }
        
    }
}
-(void)btnClick:(UIButton *)button
{
 
    _selectedButton.selected=NO;
    button.selected=YES;
    _selectedButton=button;
    if ([_delegate respondsToSelector:@selector(tabBar:didSelectedIndex:)]) {
        [_delegate tabBar:self didSelectedIndex:button.tag];
    }
    
//    if(_block)
//     _block(button.tag);
}


-(void)layoutSubviews
{
    [super layoutSubviews];
    
    CGFloat btnW=self.bounds.size.width/self.subviews.count;
    CGFloat btnH=self.bounds.size.height;
    CGFloat btnX=0;
    CGFloat btnY=0;
    
    //设置按钮的尺寸
    for(int i=0;i<self.subviews.count;i++)
    {
        UIButton *btn=self.subviews[i];
        btnX=i*btnW;
        btn.frame=CGRectMake(btnX, btnY, btnW, btnH);
    }
}
@end

2.2 点击TarBar按下颜色变化(TabBarButton)

#import "ILTabBarButton.h"
@implementation ILTabBarButton

-(void)setHighlighted:(BOOL)highlighted
{
    //NSLog(@"%s",__func__);
    //return [super setHighlighted:highlighted];
}
@end

2.3 TabBarController

#import "ILTabBarController.h"
#import "ILTabBar.h"

@interface ILTabBarController ()<ILTabBarDelegate>

@end

@implementation ILTabBarController

- (void)viewDidLoad {
    [super viewDidLoad];
   //创建tarBar
    ILTabBar *tabBar=[[ILTabBar alloc]init];
    //移除自带的tarBar
    [self.tabBar removeFromSuperview];
    NSLog(@"%@",self.tabBar);
    tabBar.delegate=self;
//    tabBar.block=^(int selectedIndex)
//    {
//        self.selectedIndex=selectedIndex;
//    };
    
    //尺寸
    tabBar.frame=self.tabBar.frame;
    [self.view addSubview:tabBar];
    
    //self.selectedIndex=2;  
}
//代理方法
-(void)tabBar:(ILTabBar *)tabBar didSelectedIndex:(int)index
{
    self.selectedIndex=index;   
}
@end

三 tabBar通过addTabBarButtonWithName添加按钮

3.1 自定义addTabBarButtonWithName方法

ILTabBar.h
//给外界创建按钮
-(void)addTabBarButtonWithName:(NSString *)name selName:(NSString *) selName;
ILTabBar.m
-(void)addTabBarButtonWithName:(NSString *)name selName:(NSString *)selName
{
    ILTabBarButton *btn=[ILTabBarButton buttonWithType:UIButtonTypeCustom];
    
    //设置按钮的图片
    [btn setBackgroundImage:[UIImage imageNamed:name] forState:UIControlStateNormal];
    [btn setBackgroundImage:[UIImage imageNamed:selName] forState:UIControlStateSelected];
    
    //监听按钮的点击
    [btn addTarget:self action:@selector(btnClick:) forControlEvents:UIControlEventTouchDown];
    
    [self addSubview:btn];
}

3.2 添加按钮(ILTabBarController.m)

  NSString *imageName=nil;
  NSString *selImageName=nil;
    
  for (int i=0; i<self.childViewControllers.count; i++) {
       imageName=[NSString stringWithFormat:@"TabBar%d",i+1];
       selImageName=[NSString stringWithFormat:@"TabBar%dSel",i+1];
       [tabBar addTabBarButtonWithName:imageName selName:selImageName];
  }

三 效果图

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值