(一)、自定義TabBaratom
系統默認自帶的底部TabBarcode
①、建立自定義IWTabBar,繼承UIView類orm
建立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
2、刪除系統自帶的Tabbarrem
①、能夠打印看下tabbar中都有哪些子控件it
NSLog(@"%@", self.tabBar.subviews);io
// 在該方法中打印看下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後ast