1.自定义一个底部 tabbarController ,然后自定义一个tabbarbutton ,继承UITabBar, 在控制器中使用kvc模式拿到UITabBar;
1》启动控制器 tabbar
UIWindow *window = [[UIWindow alloc]initWithFrame:[UIScreen mainScreen].bounds];
XBTabBarController *tab = [[XBTabBarController alloc]init];
window.rootViewController = tab;
self.window = window;
[self.window makeKeyAndVisible];
2》在tabbarController中 拿到tabbar
XBTabBar *tab = [[XBTabBar alloc]init];
//使用KVC模式设置tabbar
[self setValue:tab forKeyPath:@"tabBar"];
3》tabbar中定义tabbar的按钮位置,注意要在- (void)layoutSubviews中进行位置处理。
- (void)layoutSubviews{
[super layoutSubviews];
//设置+号按钮的位置
//self.plusbtn.center = CGPointMake(self.width * 0.5, self.height * 0.5);
//设置其他tabbarbutton的位置
CGFloat tabbarbuttonW =self.frame.size.width / 4;
CGFloat tabbatbuttonIndex = 0;
for ( UIView *child in self.subviews) {
Class class = NSClassFromString(@"UITabBarButton");
if ([child isKindOfClass:class]) {
// 设置宽度
CGFloat witch = tabbarbuttonW;
// 设置x
CGFloat X = tabbatbuttonIndex * tabbarbuttonW;
child.frame = CGRectMake(X, 0, witch, self.frame.size.height);
// 增加索引
tabbatbuttonIndex++;
}
}
}
4》tabbar系统会自动进行渲染,要在进行创建的时候进行禁止渲染
// 设置vcTabBarItem
- (void)setUpController:(UIViewController*)vcClack title:(NSString*)title imageName:(NSString*)imageName SelecdImage:(NSString*)SelecdImage{
vcClack.title = title;
vcClack.tabBarItem.image = [UIImage imageNamed:imageName];
UIImage * meImage = [UIImage imageNamed:SelecdImage];
vcClack.tabBarItem.selectedImage = [meImage imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
XBNavigationController *nav = [[XBNavigationController alloc]initWithRootViewController:vcClack];
[self addChildViewController:nav];
}