我们在做项目的过程中,会经常需要自定义tabbar。我今天刚好整理了一下,把自己的想法写出来和大家分享一下。
自定义Tabbar 首先我们新建一个继承于UITabBar的子类EzTabBar 

EzTabBar.h文件内容

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
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
import “EzTabBar.h”
 
#define TABBAR_INSET 12
 
  
 
@implementation EzTabBar
 
-(void)initialize {
UIImageView * barv = [[UIImageView alloc]init];
self.barImage = barv;
self.barImage.frame = CGRectMake(TABBAR_INSET, 0, SCREENWIDTH / 4.0f - (2 * TABBAR_INSET), 3);
 
self.barImage.backgroundColor = COLOR(210,0,12,1);
[self addSubview:self.barImage];
}
 
  
 
-(id)initWithCoder:(NSCoder *)aDecoder
{
self = [super initWithCoder:aDecoder];
if (self) {
[self initialize];
}
return self;
}
 
-(id)initWithFrame:(CGRect)frame {
self = [super initWithFrame:frame];
if (self) {
[self initialize];
}
return self;
}
 
-(CGSize)sizeThatFits:(CGSize)size
{
CGSize sizeThatFits = [super sizeThatFits:size];
sizeThatFits.height = 44;
 
return sizeThatFits;
}
 
-(void)layoutSubviews {
[super layoutSubviews];
[self bringSubviewToFront:self.barImage];
}
 
-(void)setSelectedItem:(UITabBarItem *)selectedItem {
[super setSelectedItem:selectedItem];
NSInteger indexOfSelectedItem = [[self items] indexOfObject:selectedItem];
[UIView animateWithDuration:.3 delay:.0 options:UIViewAnimationOptionCurveEaseInOut animations:^{
self.barImage.frame = CGRectMake(TABBAR_INSET + indexOfSelectedItem*(SCREENWIDTH / 4.0f), 0, self.barImage.frame.size.width, 3);
} completion:^(BOOL finished){
}];
}
 
@end
 
  
 
自定义TabbarViewController
 
import “EzTabBarController.h”
 
@interface EzTabBarController ()
 
@end
 
@implementation EzTabBarController
 
(id)initWithNibName:(NSString )nibNameOrNil bundle:(NSBundle )nibBundleOrNil
{
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {
// Custom initialization
}
return self;
}
 
(void)viewDidLoad
{
[super viewDidLoad];
 
UITabBar *tabBar = self.tabBar;
UITabBarItem *tabBarItem1 = [tabBar.items objectAtIndex:0];
UITabBarItem *tabBarItem2 = [tabBar.items objectAtIndex:1];
UITabBarItem *tabBarItem3 = [tabBar.items objectAtIndex:2];
UITabBarItem *tabBarItem4 = [tabBar.items objectAtIndex:3];
 
tabBarItem1.image = [[UIImage imageNamed:@”TabBarIconHome”] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
tabBarItem1.selectedImage = [[UIImage imageNamed:@”TabBarIconHomeSelected”] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
 
tabBarItem2.image = [[UIImage imageNamed:@”TabBarIconWardrobe”] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
tabBarItem2.selectedImage = [[UIImage imageNamed:@”TabBarIconWardrobeSelected”] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
 
tabBarItem3.image = [[UIImage imageNamed:@”TabBarIconMyShop”] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
tabBarItem3.selectedImage = [[UIImage imageNamed:@”TabBarIconMyShopSelected”] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
 
tabBarItem4.image = [[UIImage imageNamed:@”TabBarIconPersonal”] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
tabBarItem4.selectedImage = [[UIImage imageNamed:@”TabBarIconPersonalSelected”] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
 
self.view.backgroundColor = [UIColor whiteColor];
 
}
 
-(void)viewDidAppear:(BOOL)animated{
[super viewDidAppear:animated];
}
 
#pragma mark - Segues
 
-(void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
[super prepareForSegue:segue sender:sender];
if ([segue.identifier isEqualToString:@”SwitchToMyShopSegue”]) {
[segue.destinationViewController setUserId:@”0”];
} else if ([segue.identifier isEqualToString:@”BaseOptionsSegue”]) {
((BaseOptionsForCreationViewController*)segue.destinationViewController).isFromFloatingButton = YES;
}
}
 
@end

  

posted @ 2015-05-20 11:29 CoreEric 阅读(160) 评论(0) 推荐(0)
ASIHTTPRequest (作者:BenCopsey) 是一个使用简单,可用于各种从简单到复杂的 HTTP 请求,或者可用于处理 Amazon S3、Rackspace 等REST 服务的强大框架。不幸的是,Ben 早在 2011 年 9 月 21 日就已经声明停止开发和支持该框架。Ben 推荐... Read More
posted @ 2014-10-28 00:33 CoreEric 阅读(282) 评论(0) 推荐(0)
在项目开发的过程中,经常遇到要在调试的时候打印log,但是上线或是release 的时候不需要去显示log 的情况,此时你辛辛苦苦写了那么多的log,你要么就手动注释掉,要么就是设一个开关变量,企图用这个总开关开启。其实可以充分利用宏定义进行设置。 步骤比较简单,只需要 在ProjectName... Read More
posted @ 2015-01-08 12:10 CoreEric 阅读(455) 评论(0) 推荐(0)
c1外企必备:英文电子邮件高频句1. Initiate a meeting发起会议I would like to hold a meeting in the afternoon about our development planning for the project A.今天下午我建议我们就A项... Read More
posted @ 2014-10-24 10:52 CoreEric 阅读(529) 评论(0) 推荐(0)
一定要记住申请的过程中遇到问题致电苹果开发者客服4006701855他们会全程跟进你的注册进度。最主要他们态度很好而且很高效,超级赞工具/原料有国际支付功能的银行卡,并且里面有¥99(公司类型的账号需要支付99$)公司的Duns编码(没有的话参考下面的操作或者电话上面的客服电话他们会给你邮件详细的方... Read More
posted @ 2014-10-24 09:37 CoreEric 阅读(396) 评论(3) 推荐(0)
iOS常用的注释方法1、两连续斜杆”//”后,该行内容将变成注释 【快捷键:选中行按下 command+/】1//注释内容2、以”/*”开头,注释开始 “*/”结尾,终止注释1/*注释内容*/3、方法注释一般写在方法名前面。以”/**”开头,以”*/”结尾12/**方法注释一*/-(void)act... Read More
posted @ 2014-10-10 11:33 CoreEric 阅读(596) 评论(0) 推荐(0)
点击右上角即可分享
微信分享提示