概述
重点
- 屏幕底部最多显示5个tab,且最后一个显示为more
- tab点击之后回调事件
- 点击more,进入选取页面。可以选取第五个要显示的视图控制器,也可以点击edit进入编辑模式。
- 编辑过程中各个回调事件
示例
先看图
示例代码
关于如何配置UIWindow进行适配,参看iOS的UI-04-UIWindow
几个视图控制器类中没做处理,这里就不粘贴出来了。
AppDelegate.h
#import <UIKit/UIKit.h>
//实现UITabBarControllerDelegate代理
@interface AppDelegate : UIResponder <UIApplicationDelegate,UITabBarControllerDelegate>
//UIWindow属性
@property(retain,nonatomic) UIWindow* window;
@end
AppDelegate.m
#import "AppDelegate.h"
#import "VCFirst.h"
#import "VCSecond.h"
#import "VCThird.h"
#import "VCFourth.h"
#import "VCFifth.h"
#import "VCSixth.h"
@interface AppDelegate ()
@end
@implementation AppDelegate
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
// Override point for customization after application launch.
self.window = [[UIWindow alloc] initWithFrame:[UIScreen mainScreen].bounds];
//初始化六个视图控制器
VCFirst* vcFirst = [VCFirst new];
VCSecond* vcSecond = [VCSecond new];
VCThird* vcThird = [VCThird new];
VCFourth* vcFourth = [VCFourth new];
VCFifth* vcFifth = [VCFifth new];
VCSixth* vcSixth = [VCSixth new];
//设置title
vcFirst.title = @"first";
vcSecond.title = @"second";
vcThird.title = @"third";
vcFourth.title = @"fourth";
vcFifth.title = @"fifth";
vcSixth.title = @"sixth";
//设置背景颜色
vcFirst.view.backgroundColor = [UIColor redColor];
vcSecond.view.backgroundColor = [UIColor greenColor];
vcThird.view.backgroundColor = [UIColor blueColor];
vcFourth.view.backgroundColor = [UIColor orangeColor];
vcFifth.view.backgroundColor = [UIColor purpleColor];
vcSixth.view.backgroundColor = [UIColor brownColor];
//视图控制器添加到数组中
NSArray* array = [[NSArray alloc] initWithObjects:vcFirst,vcSecond,vcThird,vcFourth,vcFifth,vcSixth, nil];
//实例化分栏控制器
UITabBarController* tabC = [[UITabBarController alloc] init];
//设置代理(后面实现各个方法)
tabC.delegate = self;
//给分栏控制器的视图控制器赋值
tabC.viewControllers = array;
//设置我们的UIWindow的根控制器
self.window.rootViewController = tabC;
//生效并显示
[self.window makeKeyAndVisible];
return YES;
}
//已经选中
//底部tab选中状态监听
-(void) tabBarController:(UITabBarController *)tabBarController didSelectViewController:(UIViewController *)viewController{
NSLog(@"已经选中");
}
//edit点击之后各个回调
//开始编辑前调用
-(void) tabBarController:(UITabBarController *)tabBarController willBeginCustomizingViewControllers:(NSArray<__kindof UIViewController *> *)viewControllers{
NSLog(@"开始编辑前调用");
}
//结束编辑前调用
-(void) tabBarController:(UITabBarController *)tabBarController willEndCustomizingViewControllers:(NSArray<__kindof UIViewController *> *)viewControllers changed:(BOOL)changed{
NSLog(@"结束编辑前调用");
}
//已经结束调用
-(void) tabBarController:(UITabBarController *)tabBarController didEndCustomizingViewControllers:(NSArray<__kindof UIViewController *> *)viewControllers changed:(BOOL)changed{
NSLog(@"已经结束调用");
}
@end