有时系统的tabBar并不能满足我们的开发需求:

tabBar自定义-LMLPHP

这时,我们需要自定义一个tabBar。直接上代码:

 // 在tabBarController中用KVC更换掉系统tabBar
[self setValue:[[TTTabBar alloc] init] forKeyPath:@"tabBar"];

自定义tabBar:

@interface TTTabBar()
/** 相机按钮 */
@property (nonatomic, weak) UIButton *cameraButton;
@end @implementation XMGTabBar - (instancetype)initWithFrame:(CGRect)frame
{
if (self = [super initWithFrame:frame]) {// 添加相机按钮
UIButton *cameraButton = [UIButton buttonWithType:UIButtonTypeCustom];
[cameraButton setBackgroundImage:[UIImage imageNamed:@"camera"] forState:UIControlStateNormal];
[cameraButton setBackgroundImage:[UIImage imageNamed:@"camera_click"] forState:UIControlStateHighlighted];
cameraButton.size = publishButton.currentBackgroundImage.size;
[self addSubview:cameraButton];
self.cameraButton = cameraButton;
}
return self;
} - (void)layoutSubviews
{
[super layoutSubviews];
self.cameraButton.center = CGPointMake(self.width * 0.5, self.height * 0.5); // 设置UITabBarButton的frame
CGFloat buttonY = ;
CGFloat buttonW = self.width / ;
CGFloat buttonH = self.height;
NSInteger index = ;
for (UIView *button in self.subviews) {
if (![button isKindOfClass:[UIControl class]] || button == self.cameraButton) continue; // 计算按钮的x值
CGFloat buttonX = buttonW * ((index > )?(index + ):index);
button.frame = CGRectMake(buttonX, buttonY, buttonW, buttonH); // 增加索引
index++;
}
}
05-08 15:50