码迷,mamicode.com
首页 > 其他好文 > 详细

自定义TabBar

时间:2015-11-14 12:21:42      阅读:216      评论:0      收藏:0      [点我收藏+]

标签:

第一种:继承于UITabBarController的自定义

- (void)viewDidLoad
{
    [super viewDidLoad];
    [self creatViewController];
    self.tabBar.hidden =YES;//隐藏系统Tabbar

//方法一种的以下控件不建议用xib拖拽;
    UIView *smallView =[[UIView alloc] initWithFrame:CGRectMake(0, 518, 320, 50)];
    smallView.backgroundColor =[UIColor redColor];
    [self.view addSubview:smallView];
    [smallView release];
    UIImageView *smallImageView =[[UIImageView alloc] initWithFrame:smallView.bounds];
    smallImageView.image =[UIImage  imageNamed:@"tabBar"];
    [smallView addSubview:smallImageView];
    [smallImageView release];
    
    
//    x =  row +(row+kuang)*(i%3)
//    y =  lierow +(lierow+gao)*(i/3)
    for (int i =0 ;i<5;i++)
    {
        UIButton *btn = [UIButton buttonWithType:UIButtonTypeSystem];
        btn.frame =CGRectMake(20+60*i, 5, 40, 40);
        btn.tag =i+1;
       
        [btn setImage:[UIImage imageNamed: [NSString stringWithFormat:@"%d.png",i+1]] forState:UIControlStateNormal];
        [btn addTarget:self action:@selector(btnCilck:) forControlEvents:UIControlEventTouchUpInside];
        [smallView addSubview:btn];
    }
    
}
-(void)creatViewController
{
    UIViewController *viewController1 =[[UIViewController alloc] init];
    viewController1.view.backgroundColor  =[UIColor redColor];
    UIViewController *viewController2 =[[UIViewController alloc] init];
    viewController2.view.backgroundColor  =[UIColor blueColor];
    UIViewController *viewController3 =[[UIViewController alloc] init];
    viewController3.view.backgroundColor  =[UIColor blackColor];
    UIViewController *viewController4 =[[UIViewController alloc] init];
    viewController4.view.backgroundColor  =[UIColor greenColor];
    UIViewController *viewController5 =[[UIViewController alloc] init];
    viewController5.view.backgroundColor  =[UIColor grayColor];
    self.viewControllers=@[viewController1,viewController2,viewController3,viewController4,viewController5];
    [viewController1 release];
    [viewController2 release];
    [viewController3 release];
    [viewController4 release];
    [viewController5 release];
    
}
-(void)btnCilck:(UIButton *)btn
{
    self.selectedIndex=btn.tag-1;
}

@end

 

 

第二种 继承于UIViewController 的自定义

 

技术分享

@interface CustomViewController : UIViewController
@property (nonatomic,retain)NSArray *viewControllers;
- (IBAction)btnCilck:(id)sender; //xib连线方法声明
@property (nonatomic,assign)NSInteger selectedIndex;

@end

 

技术分享

 

//xib中的控件可与第一种方法中一样手写

 

技术分享

 

 

 

 

 

 

 

技术分享


- (void)viewDidLoad
{
    [super viewDidLoad];
    [self creatViewController];
    if (_selectedIndex>[self.viewControllers count]||_selectedIndex<0)
    {
        _selectedIndex =0;
        
    }
    //根据索引显示当前页面
    UIViewController *viewController1 =self.viewControllers[_selectedIndex];
    [self.view addSubview: viewController1.view];
    
    //视图放在底层,避免遮盖TabBar;
    [self.view sendSubviewToBack:viewController1.view];
    
}
-(void)creatViewController
{
    UIViewController *viewController1 =[[UIViewController alloc] init];
    viewController1.view.backgroundColor  =[UIColor redColor];


    UIViewController *viewController2 =[[UIViewController alloc] init];
    viewController2.view.backgroundColor  =[UIColor blueColor];


    UIViewController *viewController3 =[[UIViewController alloc] init];
    viewController3.view.backgroundColor  =[UIColor blackColor];


    UIViewController *viewController4 =[[UIViewController alloc] init];
    viewController4.view.backgroundColor  =[UIColor greenColor];


    UIViewController *viewController5 =[[UIViewController alloc] init];
    viewController5.view.backgroundColor  =[UIColor grayColor];
    
    self.viewControllers=@[viewController1,viewController2,viewController3,viewController4,viewController5];
    
    NSLog(@"%ld",self.viewControllers.count);
    [viewController1 release];
    [viewController2 release];
    [viewController3 release];
    [viewController4 release];
    [viewController5 release];
    
}

//系统方法,设置选中的索引
-(void)setSelectedIndex:(NSInteger)selectedIndex
{
    if (selectedIndex ==_selectedIndex) {
        return;
    }
    else
    {
        //移除之前加载的视图
        UIViewController *viewController =self.viewControllers[_selectedIndex];
        [viewController.view removeFromSuperview];
        
        _selectedIndex =selectedIndex;
        
        UIViewController *viewController1 =self.viewControllers[_selectedIndex];
        [self.view addSubview: viewController1.view];
        [self.view sendSubviewToBack:viewController1.view];
    }
}
- (IBAction)btnCilck:(id)sender
{
    UIButton *btn =(UIButton *)sender;
    self.selectedIndex =btn.tag-1
    ;
}
@end


 

 

自定义TabBar

标签:

原文地址:http://www.cnblogs.com/LkBolg/p/4964113.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!