(一)、自定义TabBar
系统默认自带的底部TabBar
①、创建自定义IWTabBar,继承UIView类
创建IWTabBar.m
//
// IWTabBar.m
// ItcastWeibo
//
// Created by kaiyi on 16-1-27.
// Copyright (c) 2016年 kaiyi. All rights reserved.
//
#import "IWTabBar.h"
@implementation IWTabBar
- (id)initWithFrame:(CGRect)frame
{
self = [super initWithFrame:frame];
if (self) {
// Initialization code
}
return self;
}
-(void)addTabBarButtonWithItem:(UITabBarItem *)item
{
// 1.创建按钮
UIButton *button = [[UIButton alloc] init];
[self addSubview:button];
// 2.设置数据
[button setTitle:item.title forState:UIControlStateNormal];
[button setImage:item.image forState:UIControlStateNormal];
[button setImage:item.selectedImage forState:UIControlStateSelected];
}
// 按钮frame
-(void)layoutSubviews
{
[super layoutSubviews];
CGFloat buttonH = self.frame.size.height;
CGFloat buttonW = self.frame.size.width / self.subviews.count;
CGFloat buttonY = 0;
for(int index = 0; index < self.subviews.count; index++){
// 1.取出按钮
UIButton *button = self.subviews[index];
// 2.设置按钮的frame
CGFloat buttonX = index * buttonW;
button.frame = CGRectMake(buttonX, buttonY, buttonW, buttonH);
}
}
@end
②、在IWTabBarViewController.m中引入创建的自定义IWTabBar.h文件
//
// IWTabBarViewController.m
// ItcastWeibo
//
// Created by kaiyi on 16-1-20.
// Copyright (c) 2016年 kaiyi. All rights reserved.
//
#import "IWTabBarViewController.h"
#import "IWHomeViewController.h"
#import "IWMessageViewController.h"
#import "IWDiscoverViewController.h"
#import "IWMeViewController.h"
#import "UIImage+KY.h"
#import "IWTabBar.h" // TabBar
@interface IWTabBarViewController ()
/**
* 自定义的tabbar
*/
@property (nonatomic, weak) IWTabBar *customTabBar;
@end
@implementation IWTabBarViewController
- (void)viewDidLoad
{
[super viewDidLoad];
// 初始化TabBar
[self setupTabbar];
// 初始化所有的子控制器
[self setupAllChildViewControllers];
}
// 在该方法中打印看下tabbar中都有哪些子控件,打印查看,然后移除
-(void)viewWillAppear:(BOOL)animated
{
[super viewWillAppear:animated];
// NSLog(@"%@", self.tabBar.subviews);
// 移除自带的Tabbar,保留自定义的customTabbar
for(UIView *child in self.tabBar.subviews){
// NSLog(@"%@", child.superclass);
if([child isKindOfClass:[UIControl class]])
{
[child removeFromSuperview];
}
}
}
// 初始化TabBar
-(void)setupTabbar
{
IWTabBar *customTabBar = [[IWTabBar alloc] init];
customTabBar.backgroundColor = [UIColor redColor];
// customTabBar.frame = self.tabBar.frame;
// [self.view addSubview:customTabBar];
// 自定义的tabbar完全盖住原有默认的tabbar
customTabBar.frame = self.tabBar.bounds;
[self.tabBar addSubview:customTabBar];
self.customTabBar = customTabBar;
}
// 将所有的初始化自控制器抽出成一个方法,然后在启动时自动调用
-(void)setupAllChildViewControllers
{
// 初始化所有的子控制器(首页,消息,广场,我)每个控制器的内容都不一样,所以每个自控制器实现自己的业务逻辑
// 1.首页
IWHomeViewController *home = [[IWHomeViewController alloc] init];
// 自定义的重构的公共方法
[self setupChildViewController:home title:@"首页" imageName:@"tabbar_home" selectedImageName:@"tabbar_home_selected"];
// 2.消息
IWMessageViewController *message = [[IWMessageViewController alloc] init];
// 自定义的重构的公共方法
[self setupChildViewController:message title:@"消息" imageName:@"tabbar_message_center" selectedImageName:@"tabbar_message_center_selected"];
// 3.发现
IWDiscoverViewController *discover = [[IWDiscoverViewController alloc] init];
// 自定义的重构的公共方法
[self setupChildViewController:discover title:@"广场" imageName:@"tabbar_discover" selectedImageName:@"tabbar_discover_selected"];
// 4.我
IWMeViewController *me = [[IWMeViewController alloc] init];
[self setupChildViewController:me title:@"我" imageName:@"tabbar_profile" selectedImageName:@"tabbar_profile_selected"];
}
/**
*(重构方法)初始化一个子控制器
*
* @param childVc 需要初始化的子控制器
* @param title 标题
* @param imageName 图标
* @param selectedImageName 选中的图标
*
*/
-(void)setupChildViewController:(UIViewController *)childVc title:(NSString *)title imageName:(NSString *)imageName
selectedImageName:(NSString *)selectedImageName
{
// 1.设置控制器的一些属性
childVc.title = title;
// 设置图标
childVc.tabBarItem.image = [UIImage imageWithName:imageName];
// 设置选中的图标
if(IOS7)
{
// 如果为IOS7不要渲染为蓝色, imageWithRenderingMode,注意:该方法在IOS6中报错。==== imageWithName 使用 ===
childVc.tabBarItem.selectedImage = [[UIImage imageWithName:selectedImageName] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
}
else
{
// IOS6中不需要对选中的图片进行渲染
childVc.tabBarItem.selectedImage = [UIImage imageWithName:selectedImageName];
}
// 2.包装一个导航控制器
UINavigationController *Nav = [[UINavigationController alloc] initWithRootViewController:childVc];
[self addChildViewController:Nav]; // 添加子控制器
// 3.添加tabbar内部的按钮
[self.customTabBar addTabBarButtonWithItem:childVc.tabBarItem];
}
@end
二、删除系统自带的Tabbar
①、可以打印看下tabbar中都有哪些子控件
NSLog(@"%@", self.tabBar.subviews);
// 在该方法中打印看下tabbar中都有哪些子控件,打印查看,然后移除
-(void)viewWillAppear:(BOOL)animated
{
[super viewWillAppear:animated];
// NSLog(@"%@", self.tabBar.subviews);
// 移除自带的Tabbar,保留自定义的customTabbar
for(UIView *child in self.tabBar.subviews){
// NSLog(@"%@", child.superclass);
if([child isKindOfClass:[UIControl class]])
{
[child removeFromSuperview];
}
}
}
移除默认TabBar后
来源:oschina
链接:https://my.oschina.net/u/2557944/blog/610672