自定义TabBar

1.删除自己的TabBar

- (void)setUpTabBar

{

[self.tabBar removeFromSuperview];

HANTabBar *tabBar = [[HANTabBar alloc] init];

tabBar.frame = self.tabBar.frame;

tabBar.items = self.items;

tabBar.delegate = self;

[self.view addSubview:tabBar];

}

2.自定义TabBar继承UIView并设置代理(通过代理传递选中了那个UITabBarItem)

#import

@class HANTabBar;

@protocol HANTabBarDelegate

@optional

- (void)tabBar:(HANTabBar *)tabBar index:(NSInteger)index;

@end

@interface HANTabBar : UIView

@property(nonatomic,strong)NSArray *items;

//代理

@property(nonatomic,weak)id delegate;

@end

3、添加自定义的TabBarItem(UIBUtton)添加到自定义的TabBar上

- (void)setItems:(NSArray *)items

{

_items = items;

int i = 0;

for (UITabBarItem *ite in items) {

HANTabButton *button = [[HANTabButton alloc] init];

[button setTitle:ite.title forState:UIControlStateNormal];

[button addTarget:self action:@selector(buttonClick:) forControlEvents:UIControlEventTouchUpInside];

button.tag = i;

[self addSubview:button];

i++;

}

}

4、布局添加的Button

- (void)layoutSubviews

{

CGFloat buttonW = self.frame.size.width / self.items.count;

CGFloat buttonH = self.frame.size.height;

int i = 0;

for (UIButton *button in self.subviews) {

button.frame = CGRectMake(i * buttonW, 0, buttonW, buttonH);

i++;

}

}

演示代码下载地址:https://yunpan.cn/cqkWwhUVxqQYK(提取码:0a3d)

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容