2 自定义TabBar
自定义TabBar可以完全按照我们的需求来布局和配置TabBar中各子控件的属性和布局。
@interface XMGTabBarController ()
@end
@implementation XMGTabBarController
#pragma mark - 初始化
- (void)viewDidLoad {
[super viewDidLoad];
/**** 设置所有UITabBarItem的文字属性 ****/
//省略
/**** 添加子控制器 ****/
[self setupOneChildViewController:[[UITableViewController alloc] init] title:@"精华" image:@"tabBar_essence_icon" selectedImage:@"tabBar_essence_click_icon"];
[self setupOneChildViewController:[[UITableViewController alloc] init] title:@"新帖" image:@"tabBar_new_icon" selectedImage:@"tabBar_new_click_icon"];
[self setupOneChildViewController:[[UIViewController alloc] init] title:@"关注" image:@"tabBar_friendTrends_icon" selectedImage:@"tabBar_friendTrends_click_icon"];
[self setupOneChildViewController:[[UITableViewController alloc] init] title:@"我" image:@"tabBar_me_icon" selectedImage:@"tabBar_me_click_icon"];
/**** 更换TabBar ****/
[self setValue:[[XMGTabBar alloc] init] forKeyPath:@"tabBar"];
}
@end
下面的代码是我们自定义TabBar的.m文件的主要内容,主要是重写其 layoutSubviews 方法,在该方法中我们是将四个按钮的大小和布局进行了调整,然后在最中间添加一个【发布】按钮。同样的,也有几点需要注意的:
【发布】按钮的初始化还是和上面一样,应该采用单例模式进行初始化,具体就不展开;重写 layoutSubviews 方法时,应该先调用其父类的此方法 [super layoutSubviews]; ,这样可以先对TabBarItem进行布局,然后在此布局的基础上进行布局调整。调用父类布局方法的语句不能放在后面,更不能省略,因为此方法除了对TabBarItem进行布局之外还有很多其他的配置;通过 self.subviews 来获取当前的子控件,我们可以先进行打印了解当前子控件的类型和数量,然后进行适当的筛选出我们需要修改的控件进行布局,我们一般这里采用KVC的方法进行获取和修改,例如上面我们修改当前的TabBar [self setValue:[[XMGTabBar alloc] init] forKeyPath:@"tabBar"]; ,关于如何获取属性和成员变量可以参见:runtime获取属性和成员变量
#import "XMGTabBar.h"
@interface XMGTabBar()
/** 中间的发布按钮 */
@property (nonatomic, weak) UIButton *publishButton;
@end
@implementation XMGTabBar
#pragma mark - 懒加载
/** 发布按钮 */
- (UIButton *)publishButton
{
if (!_publishButton) {
UIButton *publishButton = [UIButton buttonWithType:UIButtonTypeCustom];
publishButton.backgroundColor = XMGRandomColor;
[publishButton setImage:[UIImage imageNamed:@"tabBar_publish_icon"] forState:UIControlStateNormal];
[publishButton setImage:[UIImage imageNamed:@"tabBar_publish_click_icon"] forState:UIControlStateHighlighted];
[publishButton addTarget:self action:@selector(publishClick) forControlEvents:UIControlEventTouchUpInside];
[self addSubview:publishButton];
_publishButton = publishButton;
}
return _publishButton;
}
#pragma mark - 初始化
/**
* 布局子控件
*/
- (void)layoutSubviews
{
[super layoutSubviews];
/**** 设置所有UITabBarButton的frame ****/
// 按钮的尺寸
CGFloat buttonW = self.frame.size.width / 5;
CGFloat buttonH = self.frame.size.height;
CGFloat buttonY = 0;
// 按钮索引
int buttonIndex = 0;
for (UIView *subview in self.subviews) {
// 过滤掉非UITabBarButton
// if (![@"UITabBarButton" isEqualToString:NSStringFromClass(subview.class)]) continue;
if (subview.class != NSClassFromString(@"UITabBarButton")) continue;
// 设置frame
CGFloat buttonX = buttonIndex * buttonW;
if (buttonIndex >= 2) { // 右边的2个UITabBarButton
buttonX += buttonW;
}
subview.frame = CGRectMake(buttonX, buttonY, buttonW, buttonH);
// 增加索引
buttonIndex++;
}
/**** 设置中间的发布按钮的frame ****/
self.publishButton.frame = CGRectMake(0, 0, buttonW, buttonH);
self.publishButton.center = CGPointMake(self.frame.size.width * 0.5, self.frame.size.height * 0.5);
}
#pragma mark - 监听
- (void)publishClick
{
XMGLogFunc
}
@end










