使用步骤:
1.初始化UITabBarController
2.设置UIWindow的rootViewController为UITabBarController
3.创建相应的子控制器(ViewController)
4.把子控制器添加到UITabBarController
代码示例:
AppDelegate
- (
BOOL
)application:(
UIApplication
*)application didFinishLaunchingWithOptions:(
NSDictionary
*)launchOptions {
// 创建 window
self . window = [[ UIWindow alloc] initWithFrame:[[UIScreenmainScreen] bounds]];
//设置背景色
self.window.backgroundColor = [UIColorwhiteColor];
self. window .rootViewController = [[UIViewControlleralloc] init];
//设置window 为主窗口并显示出来
[self.windowmakeKeyAndVisible];
tabBarVc *vc = [[tabBarVcalloc]init];
//设置控制器为window的根控制器
self.window.rootViewController =vc;
returnYES;
// 创建 window
self . window = [[ UIWindow alloc] initWithFrame:[[UIScreenmainScreen] bounds]];
//设置背景色
self.window.backgroundColor = [UIColorwhiteColor];
self. window .rootViewController = [[UIViewControlleralloc] init];
//设置window 为主窗口并显示出来
[self.windowmakeKeyAndVisible];
tabBarVc *vc = [[tabBarVcalloc]init];
//设置控制器为window的根控制器
self.window.rootViewController =vc;
returnYES;
}
tabBarVc.m
- (
void
)viewDidLoad {
[superviewDidLoad];
[selfsetupViewControllers];
//选择页面的下标index
self.selectedIndex = 2;
}
-( void )setupViewControllers
{
-( void )setupViewControllers
{
oneVC *vc1 = [[oneVCalloc]init];
//这里也可以使用navigation,取决于项目的需要
// UINavigationController *nav1 = [[UINavigationController alloc] initWithRootViewController:vc1];
twoVC *vc2 = [[twoVCalloc]init];
// UINavigationController *nav2 = [[UINavigationController alloc]initWithRootViewController:vc2];
threeVC *vc3 = [[threeVCalloc]init];
// UINavigationController *nav3 = [[UINavigationController alloc]initWithRootViewController:vc3];
self.viewControllers = @[vc1,vc2,vc3];
vc1.tabBarItem.title = @"one";
vc2.tabBarItem.title = @"two";
twoVC *vc2 = [[twoVCalloc]init];
// UINavigationController *nav2 = [[UINavigationController alloc]initWithRootViewController:vc2];
threeVC *vc3 = [[threeVCalloc]init];
// UINavigationController *nav3 = [[UINavigationController alloc]initWithRootViewController:vc3];
self.viewControllers = @[vc1,vc2,vc3];
vc1.tabBarItem.title = @"one";
vc2.tabBarItem.title = @"two";
vc3.tabBarItem.title = @"three";
//tarBarItem文字和图片填充色
self.tabBar.tintColor = [UIColorredColor];
//bar背景色
self.tabBar.barTintColor = [UIColorwhiteColor];
//普通状态下的图片
vc1.
tabBarItem
.
image
= [
UIImage
imageNamed:@"image1"];
//选中状态下的图片
vc1.
tabBarItem
.
selectedImage
= [
UIImage
imageNamed:@"image2"];
vc2.tabBarItem.image = [UIImageimageNamed:@"image3"];
vc3.tabBarItem.image = [UIImageimageNamed:@"image4"];
vc2.tabBarItem.image = [UIImageimageNamed:@"image3"];
vc3.tabBarItem.image = [UIImageimageNamed:@"image4"];
}
效果如下:
点击one时 图标会切换为image2.
UITabBar上面显示的每一个Tab都对应着一个ViewController,我们可以通过设置viewcontroller.tabBarItem属性来改变tabbar上对应的tab显示内容。否则系统将会根据viewController的title自动创建一个,该tabBarItem只显示文字,没有图像。当我们自己创建UITabBarItem的时候,我们可以显示的指定显示的图像和对应的文字描述。当然还可以通过setFinishedSelectedImage:withFinishedUnselectedImage:方法给选中状态和飞选中状态指定不同的图片。下面看个自己创建UITabBarItem的小例子:
UITabBarItem *item = [[UITabBarItem alloc] initWithTitle:@"Second" image:nil tag:2]; [item setFinishedSelectedImage:[UIImage imageNamed:@"second.png"] withFinishedUnselectedImage:[UIImage imageNamed:@"first.png"]]; viewController2.tabBarItem = item;
自定义一个item同时赋值给viewController2的tabBarItem上
此外UITabBarItem还有一个属性badgeValue,通过设置该属性可以在其右上角显示一个小的角标,通常用于提示用户有新的消息
UITabBarController的Rotation
UITabBarController默认只支持竖屏,当设备方向放生变化时候,它会查询viewControllers中包含的所有ViewController,仅当所有的viewController都支持该方向时,UITabBarController才会发生旋转,否则默认的竖向。
此处需要注意当UITabBarController支持旋转,而且发生旋转的时候,只有当前显示的viewController会接收到旋转的消息。