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

一 概述

TabBar 图片尺寸引起的问题

不超过TabBar尺寸 超过TabBar尺寸

二 自定义TabBar

2.1 自定义TabBar

TabBar.h

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
#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

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
#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)

1
2
3
4
5
6
7
8
9
#import "ILTabBarButton.h"
@implementation ILTabBarButton

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

2.3 TabBarController

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
#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

1
2
//给外界创建按钮
-(void)addTabBarButtonWithName:(NSString *)name selName:(NSString *) selName;

ILTabBar.m

1
2
3
4
5
6
7
8
9
10
11
12
13
-(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)

1
2
3
4
5
6
7
8
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];
}

三 效果图