自定義tabbar(storyBoard)

 開發中系統提供的tabbar很難知足咱們的項目需求,所以咱們須要自定義。下面提供了一種簡單自定義tabbar。框架

1.storyBoard中拖拽幾個頁面ide

file:///Users/dingyunfei/Desktop/屏幕快照%202015-11-30%20下午10.45.08.png佈局

2.建立CustomViewController類代碼以下atom

//orm

//  CustomViewController.hblog

//  SB框架搭建事件

//開發

//  Created by 丁雲飛 on 15/11/26.rem

//  Copyright © 2015年 DLS. All rights reserved.get

//

 

#import <UIKit/UIKit.h>

 

@interface CustomViewController : UITabBarController

 

@end

 實現.m

#import "CustomViewController.h"

#import "CustabBar.h"

 

@interface CustomViewController ()<DSTabbarItemViewDelegate>

 

@end

 

@implementation CustomViewController

 

- (void)viewDidLoad {

    [super viewDidLoad];

    [self.tabBar removeFromSuperview];

    CustabBar *tabBar = [[CustabBar alloc] initWithFrame:CGRectMake(0, self.view.frame.size.height - 49, 320, 49)];

    tabBar.delegate = self;

//    tabBar.frame = CGRectMake(0, self.view.frame.size.height - 49, 320, 49);

    [self.view addSubview:tabBar];

}

#pragma mark -- DSTabBarItemViewDelegate

/**

 *  切換控制器

 *

 *  @param tag 控制器

 */

-(void)didSelectedTabbarItem:(NSInteger)tag {

    self.selectedIndex = tag-1; 

}

//自定義tabBar

#import <UIKit/UIKit.h>

#import "cusbutton.h"

@class CustabBar;

@protocol  DSTabbarItemViewDelegate <NSObject>

//切換控制器

-(void)didSelectedTabbarItem:(NSInteger)tag;

@end

@interface CustabBar : UIView

{

//    NSInteger select;

 

}

@property (nonatomic, unsafe_unretained)id<DSTabbarItemViewDelegate>delegate;

/**

 *  selectBtn記錄當前點擊的button。

 */

@property (nonatomic, weak)UIButton*selectBtn;

@end

 

 實現.m

#import "CustabBar.h"

@implementation CustabBar

-(instancetype)initWithFrame:(CGRect)frame{

    self = [super initWithFrame:frame];

    if (self) {

        [self addBtn];

//        self.backgroundColor = [UIColor greenColor];

    }

    return self;

}

-(void)addBtn{

    static NSString *imageName = nil;

    static NSString *selImageName = nil;

    CGFloat btnW = self.frame.size.width/4;

    CGFloat btnH = 49;

    CGFloat btnX = 0;

    CGFloat btnY = self.frame.size.height - 49;

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

    {

        imageName = [NSString stringWithFormat:@"0%d_nor",i + 1];

         selImageName = [NSString stringWithFormat:@"0%d_pre",i + 1];

        cusbutton *btn = [cusbutton buttonWithType:UIButtonTypeCustom];

        btnX = btnW *i;

        btn.frame = CGRectMake(btnX, btnY, btnW, btnH);

        btn.tag = i + 1;

        [btn setBackgroundImage:[UIImage imageNamed:imageName] forState:UIControlStateNormal];

        [btn setBackgroundImage:[UIImage imageNamed:selImageName] forState:UIControlStateSelected];

        [btn addTarget:self action:@selector(btnClick:) forControlEvents:UIControlEventTouchUpInside];

        [self addSubview:btn];

        //設置第一個爲默認控制器

        if (i == 0) {

            [self btnClick:btn];

            //此處不能用btn.selected = YES,點擊其餘按鈕時默認的按鈕沒有取消。

            

        }

    }

}

-(void)btnClick:(cusbutton *)btn{

    //_selectBtn.selected = NO上一個按鈕取消

    _selectBtn.selected = NO;

    btn.selected = YES;

    _selectBtn = btn;

    //處理點擊事件

    [self.delegate didSelectedTabbarItem:btn.tag];

}

//佈局時調用。

-(void)layoutSubviews{

    CGFloat btnW = self.frame.size.width/4;

    CGFloat btnH = 49;

    CGFloat btnX = 0;

    CGFloat btnY = self.frame.size.height - 49;

    for (int i = 0; i < self.subviews.count; i ++) {

        btnX = btnW *i;

        cusbutton *btn = (cusbutton *)self.subviews[i];

        btn.frame = CGRectMake(btnX, btnY, btnW, btnH);

        

    }

}

 //自定義BUtton

#import <UIKit/UIKit.h>

 

@interface cusbutton : UIButton

@end

 

#import "cusbutton.h"

 

@implementation cusbutton

//重寫高亮代碼。不讓按鈕有高亮狀態

-(void)setHighlighted:(BOOL)highlighted{

    //highlighted = NO;

}

 

@end

相關文章
相關標籤/搜索