UITabBarController分栏控制器基础、UITabB

2017-10-17  本文已影响0人  李琪_59dc

UITabBarController分栏控制器基本使用

![Uploading 屏幕快照 2017-10-17 16.08.33_728174.png . . .] 屏幕快照 2017-10-17 16.08.33.png

具体使用:

//AppDelegate.m
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
    // Override point for customization after application launch.
    self.window = [[UIWindow alloc]initWithFrame:[UIScreen mainScreen].bounds];
    [self.window makeKeyAndVisible];
    
    VCFirst *vcFirst = [[VCFirst alloc]init];
    
    
    VCSecond *vcSecond = [[VCSecond alloc]init];
    vcSecond.view.backgroundColor = [UIColor yellowColor];
    
    VCThird *vcThird = [[VCThird alloc]init];
    vcThird.view.backgroundColor = [UIColor orangeColor];
    
    vcFirst.title = @"视图一";
    vcSecond.title = @"视图二";
    vcThird.title = @"视图三";
    
    vcFirst.view.backgroundColor = [UIColor blueColor];
    
    UITabBarController *tbController = [[UITabBarController alloc]init];
    //创建一个控制器数组对象
    //将所有要被分栏控制器管理的对象添加到数组中
    NSArray *arrayVC = [NSArray arrayWithObjects:vcFirst,vcSecond,vcThird, nil];
    //将分栏视图控制器管理数组赋值
    tbController.viewControllers = arrayVC;
    //将分栏控制器作为根视图
    self.window.rootViewController = tbController;
    
    //设置选中的视图控制器的索引
    //通过索引来确定默认选中的视图
    tbController.selectedIndex = 2;
    if(tbController.selectedViewController == vcThird){
        NSLog(@"当前选中的是视图三");
    }
    
    //设置分栏控制器的工具栏的透明度
    tbController.tabBar.translucent = NO;
    return YES;
}
//VCFirst.m
#import "VCFirst.h"
@interface VCFirst ()
@end

@implementation VCFirst
- (void)viewDidLoad {
    [super viewDidLoad];
    
    //创建一个分栏按钮对象 方法一
    //P1:显示的文字
    //P2:显示图标
    //P3:设置按钮的标记值
//    UITabBarItem *tabBarItem = [[UITabBarItem alloc]initWithTitle:@"111" image:[UIImage imageNamed:@"icon1"] tag:101];
//    self.tabBarItem = tabBarItem;
    
    //方法二
    UITabBarItem *tabBarItem = [[UITabBarItem alloc]initWithTabBarSystemItem:UITabBarSystemItemContacts tag:101];
    tabBarItem.badgeValue = @"22";
    self.tabBarItem = tabBarItem;
}

UITabBarControllerDelegate协议方法

在分栏控制器栏目超过5个时,就会以“更多”的方式显示出来,点击更多,支持控制器位置交换,协议提供了相应事件的响应方法:

上一篇 下一篇

猜你喜欢

热点阅读