#import <objc/runtime.h>
// 需要自定義DFTabbar楼吃,這些代碼是寫(xiě)在自定義的TabbarVC里面
#import "DFTabbar.h"
- (void)viewDidLoad {
[super viewDidLoad];
[self replaceTab];
});
- (void)replaceTab{
//使用運(yùn)行時(shí)替換系統(tǒng)的
object_setClass(self.tabBar, [DFTabbar class]);
//去掉原生tabbar分割線(xiàn)
[self.tabBar setShadowImage:[UIImage new]];
[self.tabBar setBackgroundImage:[UIImage new]];
}
#import "DFTabbar.h"
/** 增加TabBar的高度 7 */
static CGFloat increaseTabBarHeight = 7;
@implementation DFTabbar
-(instancetype)initWithFrame:(CGRect)frame{
if (self = [super initWithFrame:frame]) {
self.backgroundColor = [UIColor whiteColor];
}
return self;
}
//設(shè)置TabBar的子視圖顏色
-(void)layoutSubviews{
for (UIView *view in self.subviews) {
if (![view isKindOfClass:[DFTabView class]]) {
view.backgroundColor = [UIColor clearColor];
}
}
}
//調(diào)整高度的核心方法
-(CGSize)sizeThatFits:(CGSize)size{
CGSize sizeThatFits = [super sizeThatFits:size];
//根據(jù)你的設(shè)計(jì)稿的需要,可以寫(xiě)成固定的值怜俐,我下面的寫(xiě)法是考慮到所有機(jī)型都在默認(rèn)的高度上增加15
sizeThatFits.height= sizeThatFits.height+increaseTabBarHeight;
return sizeThatFits;
}
@end