iOS 开发中,定制 tabbar 的需求比较常见。这里总结自定义的实现。分如下几步:
自定义 DockView
- 重写 initWithFrame;
- 添加 item;
- 调整 item 的 frame;
- 监听 item 的点击事件;
- 重写 setSelectedIndex.
DockView.h
#import <UIKit/UIKit.h>
@interface DockView : UIView
@property (nonatomic, copy) void (^itemClickBlock)(int index);
@property (nonatomic, assign) int selectedIndex;
- (void) addDockItemWithIcon:(NSString *)icon selectedIcon:(NSString *)icon_selecte title:(NSString *) title;
@end
DockView.m
#import "DockView.h"
#import "DockItem.h"
@interface DockView()
{
DockItem *_currentItem;
}
@end
@implementation DockView
- (id) initWithFrame:(CGRect)frame
{
self = [super initWithFrame:frame];
if(self) {
self.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageNamed:@"tabbar_background.png"]];
}
return self;
}
#pragma mark 添加item
- (void) addDockItemWithIcon:(NSString *)icon selectedIcon:(NSString *)icon_selected title:(NSString *) title
{
DockItem *item = [DockItem buttonWithType:UIButtonTypeCustom];
[self addSubview:item];
[item setTitle:title forState:UIControlStateNormal];
[item setImage:[UIImage imageNamed:icon] forState:UIControlStateNormal];
[item setImage:[UIImage imageNamed:icon_selected] forState:UIControlStateSelected];
[item addTarget:self action:@selector(itemClick:) forControlEvents:UIControlEventTouchDown];
[self adjustDockItemsFrame];
}
- (void) itemClick:(DockItem *) item
{
_currentItem.selected = NO;
item.selected = YES;
_currentItem = item;
if (_itemClickBlock) {
_itemClickBlock(item.tag);
}
}
- (void) adjustDockItemsFrame
{
int count = self.subviews.count;
CGFloat itemWidth = self.frame.size.width / count;
CGFloat itemHeight = self.frame.size.height;
for (int i = 0; i < count; i ++) {
DockItem *item = self.subviews[i];
item.frame = CGRectMake(i * itemWidth, 0, itemWidth, itemHeight);
if (i == 0) {
item.selected = YES;
_currentItem = item;
}
item.tag = i;
}
}
#pragma mark 重写设置选中的索引方法
- (void) setSelectedIndex:(int)selectedIndex
{
if (selectedIndex < 0 || selectedIndex >= self.subviews.count) {
return;
}
_selectedIndex = selectedIndex;
DockItem *item = self.subviews[selectedIndex];
[self itemClick:item];
}
@end
自定义 DockItem 继承 UIButton
- 重写initWithFrame;
- 重写 titleRectForContentRect,调整title 的 CGRect;
- 重写 imageRectForContentRect, 调整 image 的 CGRect.
DockItem.m
#define kImageRatio 0.6
#import "DockItem.h"
@implementation DockItem
- (id) initWithFrame:(CGRect)frame
{
self = [super initWithFrame:frame];
if (self) {
// 居中
self.titleLabel.textAlignment = NSTextAlignmentCenter;
self.titleLabel.font = [UIFont systemFontOfSize:12];
self.imageView.contentMode = UIViewContentModeScaleAspectFit;
self.adjustsImageWhenHighlighted = NO;
[self setBackgroundImage:[UIImage imageNamed:@"tabbar_slider.png"] forState:UIControlStateSelected];
}
return self;
}
#pragma mark 返回按钮内部UIlabel 的边框(重写)
- (CGRect) titleRectForContentRect:(CGRect) contentRect
{
CGFloat titleY = contentRect.size.height * kImageRatio - 5;
CGFloat titleHeight = contentRect.size.height - titleY;
return CGRectMake(0, titleY, contentRect.size.width, titleHeight);
}
#pragma mark 返回按钮内部UIImageView 的边框(重写)
- (CGRect) imageRectForContentRect:(CGRect)contentRect
{
return CGRectMake(0, 0, contentRect.size.width, contentRect.size.height * kImageRatio);
}
@end
使用
- 初始化一个 DockView,并设置 frame;
- 添加 DockItem;
- 设置 itemClickBlock .
DockView *dock = [[DockView alloc] init];
dock.frame = CGRectMake(0, self.view.frame.size.height - kDockHeight, self.view.frame.size.width, kDockHeight);
[self.view addSubview:dock];
[dock addDockItemWithIcon:@"tabbar_home.png" selectedIcon:@"tabbar_home_selected.png" title:@"首页"];
[dock addDockItemWithIcon:@"tabbar_discover.png" selectedIcon:@"tabbar_discover_selected.png" title:@"消息"];
dock.itemClickBlock = ^(int index) {
NSLog(@"选中了%d", index);
};