注册 登录  
 加关注
   显示下一条  |  关闭
温馨提示!由于新浪微博认证机制调整,您的新浪微博帐号绑定已过期,请重新绑定!立即重新绑定新浪微博》  |  关闭

Tolecen's Blog

One Heart, One World

 
 
 
 
 

日志

 
 

扩展UITabBarController方法  

2012-03-20 20:56:16|  分类: iOS/Objective-C |  标签: |举报 |字号 订阅

  下载LOFTER 我的照片书  |

  某些情况下,系统自带的UITabBarController不能实现某些方法,比如点击某个Item变化图片,这时就需要自己扩展系统的方法来实现这种功能。下面是一个扩展UITabBarController的例子。新定义一个类,起名为CustomTabBar,继承UITabBarController。

//头文件

#import <UIKit/UIKit.h>

@class MainView;

@interface CustomTabBar : UITabBarController {

         NSMutableArray * buttons;

         int currentSelectedIndex;

         UIImageView   * slideView;

    UILabel * lab;

    UIImageView * imagView;

    NSArray * nameArr;

    int a;

}

@property (nonatomic ,retain NSMutableArray * buttons;

@property (nonatomic ,retain UIImageView    * slideView;

@property (nonatomic ,assign int   currentSelectedIndex;

-(void) hidesTabBar;

-(void) customTabBar;

-(void) selectedTabBarItem:(UIButton *) button;

@end



//类的实现

#import "CustomTabBar.h"

#import "MainView.h"

@implementation CustomTabBar

@synthesize buttons;

@synthesize slideView;

@synthesize currentSelectedIndex;

 

-(void) viewWillAppear:(BOOL)animated

{

    NSLog(@"%d",a);

    self.selectedIndex = a;   

    nameArr = [[NSArray alloc]initWithObjects:@"图标11.png",@"图标12.png",@"图标13.png",@"图标14.png", nil];

    NSString * str = [nameArr objectAtIndex:self.selectedIndex];

    [nameArr release];

    slideView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, 80, 49)]; //加载滑动视图

    slideView.image = [UIImage imageNamed:str];

         [self hidesTabBar];

         [self customTabBar];

}

 

//隐藏TabBar所在的视图

-(void) hidesTabBar

{

         for (UIView * view in self.view.subviews)//便利self.view上的子视图

         {

              if ([view isKindOfClass:[UITabBar class]] )//判断self.view上的子视图是否是tabbar所在的视图,若是则隐藏tabbar所在的视图

              {

                     view.hidden = YES;

                     break;

              }

         }

}

 

-(void) customTabBar

{

         ///TabBar添加背景色或者图片

         UIView * tabBarBackgroundView = [[UIView alloc] initWithFrame:self.tabBar.frame];

         UIColor * color = [[UIColor alloc] initWithPatternImage:[UIImage imageNamed:@"标题栏背景.png"]];

         [tabBarBackgroundView setBackgroundColor:color];

         [color release];

         //tabBarBackGroundView加载到self.view上去

         [self.view insertSubview:tabBarBackgroundView atIndex:1];

        

         //将滚动视图加载到tabBarBackGroundView上去

 

         //创建button,如果大于5个只显示五个,小于5个时该是几个就是几个

         int viewCount = self.viewControllers.count > 5 ? 5:self.viewControllers.count;

         //button的数量

         self.buttons = [NSMutableArray arrayWithCapacity:viewCount];

         //button的宽

         double width = 320/viewCount;

         //button的高

         double height = self.tabBar.frame.size.height;

         //遍历viewController的视图  并加载button

         for (int i=0; i<viewCount; i++)

         {

              //UIViewController * viewController = [self.viewControllers objectAtIndex:i];

              UIButton * button = [UIButton buttonWithType:

                                                  UIButtonTypeCustom];

              button.frame = CGRectMake(width*i, 0, width, height);

              [button addTarget:self action:@selector(selectedTabBarItem:) forControlEvents:UIControlEventTouchUpInside];

              button.tag = i+1;

              UILabel*label = [[UILabel alloc] initWithFrame:CGRectMake(0,height-22, width, height-22)];

              [label setBackgroundColor:[UIColor clearColor]];

        UIImageView *imageView2 = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, width, height)];

        imageView2.image = [UIImage imageNamed:[NSString stringWithFormat:@"图标%d.png",i+1]];

        [button addSubview:imageView2];

        [imageView2 release];

        if (i == 0) {

            [label setText:@"我的收藏"];

        }

        else if(i == 1) {

            [label setText:@"自选位置"];

        }

        else if(i == 2) {

            [label setText:@"当前位置"];

        }

        else {

            [label setText:@"更多"];

        }

        label.textColor = [UIColor colorWithRed:77/255.0 green:36/255.0 blue:21/255.0 alpha:1.0];

              [label setFont:[UIFont systemFontOfSize:12.0]];//改变字体大小

              [label setTextAlignment:UITextAlignmentCenter];//字体中间对齐

              [button addSubview:label];

              [label release];

             

              [self.buttons addObject:button];

              [tabBarBackgroundView addSubview:button];

//        [tabBarBackgroundView addSubview:slideView];

         }

         [tabBarBackgroundView release];

         [self selectedTabBarItem:[self.buttons objectAtIndex:self.selectedIndex]];

        

}

-(void) selectedTabBarItem:(UIButton *) button

{

    if ([imagView superview]) {

        [imagView removeFromSuperview];

    }

    if ([lab superview]) {

        [lab removeFromSuperview];

    }

    int i = button.tag;

    float width = button.frame.size.width;

    float height = button.frame.size.height;

    self.currentSelectedIndex = button.tag-1;

    self.selectedIndex = self.currentSelectedIndex;

    [self performSelector:@selector(slideTabBarItem:) withObject:imagView];

         if (self.currentSelectedIndex ==i-1) {

        NSLog(@"我变!!!!");

        imagView = [[UIImageView alloc]initWithFrame:CGRectMake(0, 0, width, height)];

        NSLog(@"图标1%d",button.tag);

        imagView.image = [UIImage imageNamed:[NSString stringWithFormat:@"图标1%d.png",button.tag]];

        [button addSubview:imagView];

        [imagView release];

        lab = [[UILabel alloc] initWithFrame:CGRectMake(0,height-22, width, height-22)];

        if (i == 1) {

            [lab setText:@"我的收藏"];

        }

        else if(i == 2) {

            [lab setText:@"自选位置"];

        }

        else if(i == 3) {

            [lab setText:@"当前位置"];

        }

        else{

            [lab setText:@"更多"];

        }

        lab.backgroundColor = [UIColor clearColor];

        lab.textColor = [UIColor whiteColor];

              [lab setFont:[UIFont systemFontOfSize:12.0]];//改变字体大小

              [lab setTextAlignment:UITextAlignmentCenter];//字体中间对齐

              [button addSubview:lab];

        [lab release];

         }

}

 

-(void) slideTabBarItem:(UIImageView *) button

{

         [UIView beginAnimations:nil context:nil];

         [UIView setAnimationCurve:0.2];

         [UIView setAnimationDelegate:self];

//       slideView.frame = button.frame;//将被点击按钮的frame赋值给tabBarViewframe,使滑动视图移到被点击的按钮上

         [UIView commitAnimations];

}


- (void)didReceiveMemoryWarning {

    // Releases the view if it doesn't have a superview.

    [super didReceiveMemoryWarning];

   

    // Release any cached data, images, etc. that aren't in use.

}

 

- (void)viewDidUnload {

    [super viewDidUnload];

    // Release any retained subviews of the main view.

    // e.g. self.myOutlet = nil;

}

- (void)dealloc {

         [slideView release];

         [buttons release];

    [super dealloc];

}

@end

  评论这张
 
阅读(2264)| 评论(0)
推荐 转载

历史上的今天

在LOFTER的更多文章

评论

<#--最新日志,群博日志--> <#--推荐日志--> <#--引用记录--> <#--博主推荐--> <#--随机阅读--> <#--首页推荐--> <#--历史上的今天--> <#--被推荐日志--> <#--上一篇,下一篇--> <#-- 热度 --> <#-- 网易新闻广告 --> <#--右边模块结构--> <#--评论模块结构--> <#--引用模块结构--> <#--博主发起的投票-->
 
 
 
 
 
 
 
 
 
 
 
 
 
 

页脚

网易公司版权所有 ©1997-2017