iOS自定义tabbar

来源:互联网 发布:淘宝卖的牛肉干 编辑:程序博客网 时间:2024/04/29 15:35


@protocol TablebarViewDelegate <NSObject> -(void)tablebarViewDelegate:(NSInteger)tag; 
@end 

@interface TablebarView : UIView 

@property(nonatomic ,strong) id<TablebarViewDelegate>delegate;

@property(nonatomic ,strong) UIButton * button1; 
@property(nonatomic ,strong) UIButton * button2; 
@property(nonatomic ,strong) UIButton * button3; 
@property(nonatomic ,strong) UIButton * button4; 
@property(nonatomic ,strong) UIButton * button5; 
@property(nonatomic ,strong) UIButton * button6; 

@end

/////////////////////////////////

// 
//  TablebarView.m 
//  chageView 
// 
//  Created by LEE on 13-10-6. 
//  Copyright (c) 2013年 LEE. All rights reserved. 
// 

#import "TablebarView.h" 

#define kScreenHeight  (kIsIphone5 ? 548 : 460) 
#define kScreenWidth  [UIScreen mainScreen].bounds.size.width 
#define STEP 5 
#define BarSection ([UIScreen mainScreen].bounds.size.width)/STEP 
@implementation TablebarView 
@synthesize delegate; 
- (id)initWithFrame:(CGRect)frame 

    self = [super initWithFrame:frame]; 
    if (self) 
    { 
         
        self.backgroundColor = [UIColor greenColor]; 
        NSLog(@"%f",[UIScreen mainScreen].bounds.size.width); 
        //button是按照从左到右的顺序排列的 
        _button1 = [UIButton buttonWithType:UIButtonTypeCustom]; 
        _button1.frame = CGRectMake(0, 0,BarSection, 44); 
        [_button1 addTarget:self action:@selector(tableBarDelegate:) forControlEvents:UIControlEventTouchUpInside]; 
        _button1.tag = 2013; 
        [self addSubview:_button1]; 
         
        _button2 = [UIButton buttonWithType:UIButtonTypeCustom]; 
        _button2.frame = CGRectMake(BarSection, 0,BarSection, 44); 
        [_button2 addTarget:self action:@selector(tableBarDelegate:) forControlEvents:UIControlEventTouchUpInside]; 
        _button2.tag = 2014; 
        [self addSubview:_button2]; 
         
        _button3 = [UIButton buttonWithType:UIButtonTypeCustom]; 
        _button3.frame = CGRectMake(BarSection*2, 0,BarSection, 44); 
        [_button3 addTarget:self action:@selector(tableBarDelegate:) forControlEvents:UIControlEventTouchUpInside]; 
        _button3.tag = 2015; 
        [self addSubview:_button3]; 
         
        _button4 = [UIButton buttonWithType:UIButtonTypeCustom]; 
        _button4.frame = CGRectMake(BarSection *3, 0,BarSection, 44); 
        [_button4 addTarget:self action:@selector(tableBarDelegate:) forControlEvents:UIControlEventTouchUpInside]; 
        _button4.tag = 2016; 
        [self addSubview:_button4]; 
         
        _button5 = [UIButton buttonWithType:UIButtonTypeCustom]; 
        _button5.frame = CGRectMake(BarSection *4, 0,BarSection, 44); 
        [_button5 addTarget:self action:@selector(tableBarDelegate:) forControlEvents:UIControlEventTouchUpInside]; 
        _button5.tag = 2017; 
        [self addSubview:_button5]; 
         
        _button6 = [UIButton buttonWithType:UIButtonTypeCustom]; 
        _button6.frame = CGRectMake(BarSection *5, 0,BarSection, 44); 
        [_button6 addTarget:self action:@selector(tableBarDelegate:) forControlEvents:UIControlEventTouchUpInside]; 
        _button6.tag = 2018; 
        [self addSubview:_button6]; 
   
    } 
    return self; 


-(void)tableBarDelegate:(UIButton *)buttonTag 

    [delegate tablebarViewDelegate:buttonTag.tag]; 


@end 

/////////////////////////////////////

// 
//  changeViewViewController.h 
//  chageView 
// 
//  Created by LEE on 13-10-3. 
//  Copyright (c) 2013年 LEE. All rights reserved. 
// 

#import <UIKit/UIKit.h> 

@interface changeViewViewController : UIViewController 
@property(nonatomic ,strong) UIButton * btn1; 
@property(nonatomic ,strong) UIButton * btn2; 
@property(nonatomic ,strong) UIScrollView * scrowView; 
@end 

////////////////////////

// 
//  changeViewViewController.m 
//  chageView 
// 
//  Created by LEE on 13-10-3. 
//  Copyright (c) 2013年 LEE. All rights reserved. 
//

#import "changeViewViewController.h" 
#import "redController.h" 
#import "blueController.h" 
#import "TablebarView.h" 

@interface changeViewViewController ()<UIScrollViewDelegate,TablebarViewDelegate>

@end 

@implementation changeViewViewController 

- (void)viewDidLoad 

    [super viewDidLoad]; 
     
    _scrowView = [[UIScrollView alloc]init]; 
    _scrowView.frame = CGRectMake(0, 0, 320, self.view.frame.size.height); 
    _scrowView.contentSize = CGSizeMake(10, 1000); 
    _scrowView.delegate=self; 
    _scrowView.backgroundColor = [UIColor redColor]; 
     
    //将滚动视图添加到当前的视图上 
    [self.view addSubview:_scrowView]; 
    //给滚动视图添加tableBar 
    TablebarView * tableBar = [[TablebarView alloc]init]; 
    tableBar.frame = CGRectMake(0, 100, self.view.frame.size.width, 44); 
    tableBar.button1.backgroundColor = [UIColor blackColor]; 
    tableBar.button2.backgroundColor = [UIColor whiteColor]; 
    tableBar.button3.backgroundColor = [UIColor blueColor]; 
    tableBar.button4.backgroundColor = [UIColor greenColor]; 
    tableBar.button5.backgroundColor = [UIColor brownColor]; 
    tableBar.button6.backgroundColor = [UIColor purpleColor]; 
    tableBar.delegate = self; 
    [_scrowView addSubview:tableBar]; 
     


-(void)tablebarViewDelegate:(NSInteger)tag 

    switch (tag) { 
        case 2013: 
        { 
            NSLog(@"%d",tag); 
        } 
            break; 
        case 2014: 
        { 
             NSLog(@"%d",tag); 
             
        } 
            break; 
        case 2015: 
        { 
             NSLog(@"%d",tag); 
        } 
            break; 
        case 2016: 
        { 
             NSLog(@"%d",tag); 
        } 
            break; 
        case 2017: 
        { 
            NSLog(@"%d",tag); 
        } 
            break; 
        case 2018: 
        { 
            NSLog(@"%d",tag); 
        } 
            break; 
             
        default: 
            break; 
    } 



- (void)didReceiveMemoryWarning 

    [super didReceiveMemoryWarning]; 
    // Dispose of any resources that can be recreated. 


@end

最后用kvc 改变。[self set value:tabor forkeyPath:@"tabbar"]; 

0 0