UITableBarController是选项卡栏导航控制器,显示效果是在页面底部,有多个选项卡,通过点击不同的选项卡可以在不同的ViewController之间切换
首先要先创建几个ViewController
在AppDelegate.m文件中
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
// Override point for customization after application launch.
//创建windou
self.window = [[UIWindow alloc] initWithFrame:[UIScreen mainScreen].bounds];
//显示window
[self.window makeKeyAndVisible];
//创建控制器一
ViewC1 *vcFirst = [[ViewC1 alloc] init];
ViewC2 *vcSecond = [[ViewC2 alloc] init];
ViewC3 *vcThird = [[ViewC3 alloc] init];
vcFirst.title = @"首页";
vcSecond.title = @"搜索";
vcThird.title = @"个人";
vcFirst.view.backgroundColor = [UIColor blueColor];
vcSecond.view.backgroundColor = [UIColor orangeColor];
vcThird.view.backgroundColor = [UIColor whiteColor];
//创建分栏控制器
UITabBarController *tb= [[UITabBarController alloc] init];
//设置分栏控制器整体颜色
tb.tabBar.tintColor = [UIColor greenColor];
//创建一个控制器数组对象
NSArray *arryVC = [NSArray arrayWithObjects:vcFirst, vcSecond, vcThird, nil];
//将分栏控制器赋值
tb.viewControllers = arryVC;
//将分栏控制器作为根控制器
self.window.rootViewController = tb;
return YES;
}
然后在ViewContrlooer中设置显示效果
#import "ViewC1.h"
@interface ViewC1 ()
@end
@implementation ViewC1
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view.
//设置点击的图像
UIImage *image = [UIImage imageNamed:@"发现.png"];
UITabBarItem *tabBarItem = [[UITabBarItem alloc] initWithTitle:@"发现" image:nil tag:101];
tabBarItem.image = [image imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
tabBarItem.selectedImage = [[UIImage imageNamed:@"音乐2.png"] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
self.tabBarItem = tabBarItem;
}
/*
#pragma mark - Navigation
// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
// Get the new view controller using [segue destinationViewController].
// Pass the selected object to the new view controller.
}
*/
@end
#import "ViewC2.h"
@interface ViewC2 ()
@end
@implementation ViewC2
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view.
UIImage *image = [UIImage imageNamed:@"视频.png"];
UITabBarItem *tabBarItem = [[UITabBarItem alloc] initWithTitle:@"视频" image:nil tag:30];
tabBarItem.image = [image imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
tabBarItem.selectedImage = [[UIImage imageNamed:@"视频2.png"] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
self.tabBarItem = tabBarItem;
}
/*
#pragma mark - Navigation
// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
// Get the new view controller using [segue destinationViewController].
// Pass the selected object to the new view controller.
}
*/
@end
#import "ViewC3.h"
@interface ViewC3 ()
@end
@implementation ViewC3
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view.
UIImage *image = [UIImage imageNamed:@"我的.png"];
UITabBarItem *tabBarItem = [[UITabBarItem alloc] initWithTitle:@"我的" image:nil tag:101];
tabBarItem.image = [image imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
tabBarItem.selectedImage = [[UIImage imageNamed:@"音符 2.png"] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
self.tabBarItem = tabBarItem;
}
显示效果如下