iOS 之自訂TableBar

來源:互聯網
上載者:User

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


#import <UIKit/UIKit.h>

-(void)tablebarViewDelegate:(NSInteger)tag;
@end

@interface TablebarView : UIView

@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;

//  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


相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.