UITabBarController

UITabBarController

概念

A container view controller that manages a radio-style selection interface, where the selection determines which child view controller to display.

一个容器视图控制器,用于管理无线电样式的选择界面,选择决定了哪一个子视图控制器展现。

声明

@interface UITabBarController : UIViewController

概述

标签栏控制器页面的每一个标签都与一个自定义的视图控制器相关联。可以将你所要展现的视图控制器加入到viewcontrollers属性,指定控制器的顺序决定了在标签栏页面的顺序。也可以使用setViewControllers方法实现视图控制器的添加。

// 第一种
[tabBarController setViewControllers:@[controller1, controller2, controller3, controller4]];
// 第二种
    tabBarController.viewControllers = @[controller1, controller2, controller3, controller4];

要将标签栏项目与视图控制器相关联,请创建UITabBarItem类的新实例为视图控制器进行适当配置,然后将其分配给视图控制器的tabBarItem属性或者直接配置视图控制器的tabBarItem属性。如果您不为视图控制器提供自定义标签栏项目,则视图控制器会创建一个默认项目,其中不包含图像和视图控制器的title属性中的文本。

UIViewController *controller1 = [[UIViewController alloc] init];
controller1.view.backgroundColor = [UIColor yellowColor];
controller1.tabBarItem.title = @"新闻";
controller1.tabBarItem.image = [UIImage systemImageNamed:@"tv"];

因为UITabBarController类继承自UIViewController类,所以选项卡栏控制器具有自己的视图,可通过view属性访问该视图。 标签栏控制器的视图只是包含标签栏视图和自定义视图的容器。上图整个window的视图控制器结构。

例子

创建了一个带有4个标签的标签控制器。

#import "ViewController.h"
#import "AppDelegate.h"

@interface AppDelegate ()

@end

@implementation AppDelegate


- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
    self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
    
    
    
    UITabBarController *tabBarController = [[UITabBarController alloc] init];
    
    
    UIViewController *controller1 = [[UIViewController alloc] init];
    controller1.view.backgroundColor = [UIColor yellowColor];
    controller1.tabBarItem.title = @"新闻";
    controller1.tabBarItem.image = [UIImage systemImageNamed:@"tv"];
    
    UIViewController *controller2 = [[UIViewController alloc] init];
    controller2.view.backgroundColor = [UIColor systemBlueColor];
    controller2.tabBarItem.title = @"视频";
    controller2.tabBarItem.image = [UIImage systemImageNamed:@"video"];
    
    UIViewController *controller3 = [[UIViewController alloc] init];
    controller3.view.backgroundColor = [UIColor systemPurpleColor];
    controller3.tabBarItem.title = @"推荐";
    controller3.tabBarItem.image = [UIImage systemImageNamed:@"arrow.right.square"];
    
    UIViewController *controller4 = [[UIViewController alloc] init];
    controller4.view.backgroundColor = [UIColor systemTealColor];
    controller4.tabBarItem.title = @"我的";
    controller4.tabBarItem.image = [UIImage systemImageNamed:@"person"];
    
    //[tabBarController setViewControllers:@[controller1, controller2, controller3, controller4]];
    tabBarController.viewControllers = @[controller1, controller2, controller3, controller4];
    
    self.window.rootViewController = tabBarController;
    [self.window makeKeyAndVisible];
    // Override point for customization after application launch.
    return YES;
}

@end

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值