#import <UIKit/UIKit.h>
@interface iPadCustom_TabBar : UITabBarController{
UIImageView *backgroud_image;
UIImageView *select_image;
UIImageView *tab_bar_bg;
NSMutableArray *tab_text;
NSMutableArray *tab_btn;
UIButton *btn;
}
-(void)init_tab;
-(void)when_tabbar_is_unselected;
-(void)add_custom_tabbar_elements;
-(void)when_tabbar_is_selected:(int)tabID;
@end
#import "iPadCustom_TabBar.h"
#import "AppDefination.h"
@implementation iPadCustom_TabBar
- (void)init_tab
{
backgroud_image = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"leftCheck.png"]];
select_image = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"leftUncheck.png"]];
tab_bar_bg = [[UIImageView alloc] initWithImage:[UIImageimageNamed:@"images/[email protected]"]];
// tab_text = [[NSMutableArray alloc] initWithObjects:@"默認(rèn)",@"個(gè)人",@"搜索",@"更多",
nil];
}
-(void)viewDidAppear:(BOOL)animated{
[super viewWillAppear:animated];
[self init_tab];
[self when_tabbar_is_unselected];
[self add_custom_tabbar_elements];
}
-(void)when_tabbar_is_unselected{
for (UIView *view in self.view.subviews)
{
if ([view isKindOfClass:[UITabBar class]])
{
view.hidden = YES;
break;
}
}
}
-(void)add_custom_tabbar_elements{
int tab_num = 4;
int i;
UIImageView *tabbar_bg1 = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, 85, 1024)];
// [self addSubview:theImageView];
[tabbar_bg1 setImage:tab_bar_bg.image];
[self.view addSubview:tabbar_bg1];
tab_btn = [[NSMutableArray alloc] initWithCapacity:0];
for (i=0; i < tab_num; i++)
{
btn = [UIButton buttonWithType:UIButtonTypeCustom];
[btn setFrame:CGRectMake(5,
i*100+100, 75, 75)];
[btn setBackgroundImage:backgroud_image.image forState:UIControlStateNormal];
[btn setBackgroundImage:select_image.image forState:UIControlStateSelected];
// [btn setTitle:[tab_text objectAtIndex:i] forState:UIControlStateNormal];
[btn.titleLabel setFont:[UIFont boldSystemFontOfSize:15.0]];
if (i == 0) {
[btn setSelected:YES];
}
[btn setTag:i];
[tab_btn addObject:btn];
[self.view addSubview:btn];
[btn addTarget:self action:@selector(button_clicked_tag:)forControlEvents:UIControlEventTouchUpInside];
[btn release];
}
}
-(void)button_clicked_tag:(id)sender{
int tagNum = [sender tag];
[self when_tabbar_is_selected:tagNum];
}
-(void)when_tabbar_is_selected:(int)tabID{
switch (tabID) {
case 0:
[[tab_btn objectAtIndex:0] setSelected:true];
[[tab_btn objectAtIndex:1] setSelected:false];
[[tab_btn objectAtIndex:2] setSelected:false];
[[tab_btn objectAtIndex:3] setSelected:false];
break;
case 1:
[[tab_btn objectAtIndex:0] setSelected:false];
[[tab_btn objectAtIndex:1] setSelected:true];
[[tab_btn objectAtIndex:2] setSelected:false];
[[tab_btn objectAtIndex:3] setSelected:false];
break;
case 2:
[[tab_btn objectAtIndex:0] setSelected:false];
[[tab_btn objectAtIndex:1] setSelected:false];
[[tab_btn objectAtIndex:2] setSelected:true];
[[tab_btn objectAtIndex:3] setSelected:false];
break;
case 3:
[[tab_btn objectAtIndex:0] setSelected:false];
[[tab_btn objectAtIndex:1] setSelected:false];
[[tab_btn objectAtIndex:2] setSelected:false];
[[tab_btn objectAtIndex:3] setSelected:true];
break;
}
self.selectedIndex = tabID;
}
-(void)dealloc{
[backgroud_image release];
[select_image release];
[tab_bar_bg release];
// [tab_text release];
[tab_btn release];
[super dealloc];
}
方法二:
#import <UIKit/UIKit.h>
#define NotificationUpdateBadgeValue @"NotificationUpdateBadgeValue"
@interface iPad_CustomTabBarController : UITabBarController{
NSMutableArray *buttons;
NSArray *buttonNormalImages;
NSArray *buttonSelectedImages;
int currentSelectedIndex;
}
@property (nonatomic, assign) int currentSelectedIndex;
@property (nonatomic, retain) NSArray *buttonNormalImages;
@property (nonatomic, retain) NSArray *buttonSelectedImages;
@property (nonatomic,retain) NSMutableArray *buttons;
- (void)hideRealTabBar;
- (void)customTabBar;
- (void)selectedTab:(UIButton *)button;
- (void) checkAndAddBadge:(NSNotification *) notification;
@end
#import "iPad_CustomTabBarController.h"
#import "UIBadgeView.h"
#import "AppDefination.h"
@implementation iPad_CustomTabBarController
@synthesize currentSelectedIndex;
@synthesize buttons;
@synthesize buttonSelectedImages;
@synthesize buttonNormalImages;
- (void)viewDidAppear:(BOOL)animated{
self.view.backgroundColor =
[UIColor clearColor];
[self hideRealTabBar];
[self customTabBar];
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(checkAndAddBadge:)name: NotificationUpdateBadgeValue object:nil];
[super viewDidAppear:animated];
}
- (void) hideRealTabBar {
[UIView beginAnimations:nil context:NULL];
[UIView setAnimationDuration:0.0];
for(UIView *view in self.view.subviews)
{
if([view isKindOfClass:[UITabBar class]])
{
view.hidden = YES;
} else {
[view setFrame:CGRectMake(view.frame.origin.x,
view.frame.origin.y, view.frame.size.width, 1024)];
NSLog(@"x=%f",view.frame.origin.x);
NSLog(@"x=%f",view.frame.origin.y);
NSLog(@"x=%f",view.frame.size.width);
}
}
[UIView commitAnimations];
}
- (void)customTabBar{
UIImageView *tabBarBackGroundView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0,ipadTabBarWidth, 1024)];
tabBarBackGroundView.image = [UIImage imageNamed:@"images/[email protected]"];
tabBarBackGroundView.backgroundColor = [UIColor clearColor];
[self.view addSubview:tabBarBackGroundView];
//創(chuàng)建按鈕
int viewCount = self.viewControllers.count > 5 ? 5 : self.viewControllers.count;
self.buttons = [NSMutableArray arrayWithCapacity:viewCount];
double _width = 60;
// NSLog(@"%d",viewCount);
double _height = 50;
for (int i = 0; i < viewCount; i++) {
// UIViewController *v = [self.viewControllers objectAtIndex:i];
UIButton *btn = [UIButton buttonWithType:UIButtonTypeCustom];
btn.frame = CGRectMake(10, i*(_height+30)+30,
_width, _height);
[btn addTarget:self action:@selector(selectedTab:)forControlEvents:UIControlEventTouchDown];
btn.tag = i;
// NSLog(@"%@",v.tabBarItem.image);
UIImage *image = [buttonNormalImages objectAtIndex:i];
[btn setBackgroundImage:image forState:UIControlStateNormal];
image = [buttonSelectedImages objectAtIndex:i];
[btn setBackgroundImage:image forState:UIControlStateSelected];
[btn setBackgroundImage:image forState:UIControlStateHighlighted];
[self.buttons addObject:btn];
[self.view addSubview:btn];
}
[tabBarBackGroundView release];
[self selectedTab:[self.buttons objectAtIndex:0]];
}
- (void)selectedTab:(UIButton *)button{
UIImage *activebg = [buttonSelectedImages objectAtIndex:button.tag];
[button setBackgroundImage:activebg forState:UIControlStateNormal];
NSLog(@"index=%d",self.currentSelectedIndex);
NSLog(@"button.tag=%d",button.tag);
if (self.currentSelectedIndex == button.tag){
// [[self.viewControllers objectAtIndex:button.tag] popToRootViewControllerAnimated:YES];
button.selected = YES;
return;
}
UIImage *normalbg = [buttonNormalImages objectAtIndex:self.currentSelectedIndex];
UIButton *btn = [self.buttons objectAtIndex:self.currentSelectedIndex];
[btn setBackgroundImage:normalbg forState:UIControlStateNormal];
btn.selected = NO;
self.currentSelectedIndex = button.tag;
self.selectedIndex = self.currentSelectedIndex;
}
- (void) checkAndAddBadge:(NSNotification *) notification{
NSDictionary *info = [notification userInfo];
NSString *badgeValue = [info objectForKey:@"badgeValue"] ;
if (badgeValue == nil) {
//do nothing
return;
}
int barIndex = [[info objectForKey:@"barIndex"] intValue]
;
UIButton *btn = [self.buttons objectAtIndex:barIndex];
UIBadgeView *bview = (UIBadgeView *)[btn viewWithTag:(barIndex + 10)];
if (bview != nil) {
[bview removeFromSuperview];
}
if ([badgeValue isEqualToString:@""] || [badgeValue isEqualToString:@"0"])
{
return;
}
CGSize numberSize = [badgeValue sizeWithFont: [UIFont boldSystemFontOfSize:14]];
UIBadgeView *badgeView = [[UIBadgeView alloc] initWithFrame:CGRectMake(btn.bounds.size.width -
numberSize.width - 14, 0, numberSize.width + 14, 21)];
badgeView.badgeString = badgeValue;
badgeView.badgeColor = [UIColor redColor];
badgeView.tag = barIndex + 10;
[btn addSubview:badgeView];
[badgeView release];
}
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
return YES;
}
- (void) dealloc{
[buttons release];
[buttonNormalImages release];
[buttonSelectedImages release];
[super dealloc];
}
@end
@end
來自http://blog.sina.com.cn/s/blog_4cd8dd130100uec2.html
|