您的位置:首页 > 移动开发 > IOS开发

IOS 自定义Tabbar

2016-02-17 17:45 387 查看
自定义Tabbar

1. WQTabBar.h

#import <UIKit/UIKit.h>

@class WQTabBar;
@protocol WQTabBarDelegate <NSObject>

- (void)tabbar:(WQTabBar *)tabbar didSelectedFrom:(NSInteger)from to:(NSInteger)to;

@end

@interface WQTabBar : UIView

@property (nonatomic,weak) id<WQTabBarDelegate> delegate;

- (void)addTabbarButtonWithNormalImg:(NSString *)norImg selectedImg:(NSString *)selImg;
@end

//自定义按钮
@interface WQTabButton : UIButton

@end


2. WQTabBar.m

#import "WQTabBar.h"
@interface WQTabBar()
@property(nonatomic,weak)UIButton *mSelectedButton;
@end

@implementation WQTabBar

/**
* 初始化自定的tabbar ,初始化过程中,添加子View
*/
- (instancetype)initWithFrame:(CGRect)frame{
if (self = [super initWithFrame:frame]) {
// [self setUpButtons];
}
return self;
}

- (void)addTabbarButtonWithNormalImg:(NSString *)norImg selectedImg:(NSString *)selImg{
UIButton *button = [WQTabButton buttonWithType:UIButtonTypeCustom];
[button setBackgroundImage:[UIImage imageNamed:norImg] forState:UIControlStateNormal];
[button setBackgroundImage:[UIImage imageNamed:selImg] forState:UIControlStateSelected];
button.contentMode = UIViewContentModeCenter;
[button addTarget:self action:@selector(tabbarClick:) forControlEvents:UIControlEventTouchUpInside];
button.tag = self.subviews.count;

if (self.subviews.count==0) {
self.mSelectedButton = button;
self.mSelectedButton.selected = YES;
}
[self addSubview:button];

}

/**
重新布局子控件
*/
-(void)layoutSubviews{
[super layoutSubviews];
NSInteger count = self.subviews.count;
CGFloat btnW= self.bounds.size.width/count;
CGFloat btnH = self.bounds.size.height;

for(NSInteger i = 0;i<count;i++){
UIButton *button = self.subviews[i];
button.frame = CGRectMake(btnW*i, 0, btnW, btnH);
}
}

/**
*button点击事件,并且通过代理进行事件传递
*/
-(void)tabbarClick:(UIButton *)button{
//切换tabbar的控制器
self.mSelectedButton.selected = NO;
button.selected = YES;
self.mSelectedButton = button;

if ([self.delegate respondsToSelector:@selector(tabbar:didSelectedFrom:to:)]) {
[self.delegate tabbar:self didSelectedFrom:self.mSelectedButton.tag to:button.tag];
}
}
@end

//自定义按钮,并且关闭图片点击高亮的效果
@implementation WQTabButton

-(void)setHighlighted:(BOOL)highlighted{

}

@end


3.如何使用
#import "WQTabbarController.h"
#import "WQTabBar.h"

@interface WQTabbarController ()<WQTabBarDelegate>

@end

@implementation WQTabbarController

- (void)viewDidLoad {
[super viewDidLoad];

//自定义tabbar 替换原有的tabbar
WQTabBar *tabbar = [[WQTabBar alloc]initWithFrame:self.tabBar.bounds];
tabbar.backgroundColor = [UIColor redColor];
tabbar.delegate = self;

for(NSInteger i = 0;i<4;i++){
NSString *normalImg = [NSString stringWithFormat:@"tab_normal_%ld",(i+1)];
NSString *selectImg = [NSString stringWithFormat:@"tab_selected_%ld",(i+1)];
[tabbar addTabbarButtonWithNormalImg:normalImg selectedImg:selectImg];
}
[self.tabBar addSubview:tabbar];

}

-(void)tabbar:(WQTabBar *)tabbar didSelectedFrom:(NSInteger)from to:(NSInteger)to{
self.selectedIndex = to;
}

@end
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: