自定义Tabbar #iOS

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

EzTabBar.h文件内容

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

  

郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。