TabBarController创建及使用方法简介

TabBarController创建及使用方法简介

大致讲解一下TabBarController的创建过程:

首先,我们需要一些视图,如创建UIControllerView类型的view1,view2,view3.

然后,我们需要创建 一个UITabBarController类型的实例tabBarView,然后我们将刚刚创建的View1,view2,view3添加到tabBarView中的viewcontroller这个数组中。

我们就完成了一个UITabBarController的创建

注意,一般这个TabBarControler是在appdelegate文件中创建的。因为这个TabBarController是作为我们根视图控制器使用的。

用代码在启动函数中实现一个UITabBarController实例的创建:

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
    // Override point for customization after application launch.
    NSArray *colorArray = [[NSArray alloc] initWithObjects:[UIColor redColor], [UIColor blueColor], [UIColor greenColor], [UIColor blackColor], nil];

                           
    self.TabBarCV = [[UITabBarController alloc] init];
    self.View = [[NSMutableArray alloc] init];
    
    self.TabBarCV.delegate = self;
    for (int i = 0; i < [colorArray count]; i++) {
        ViewController *view = [[ViewController alloc] initWithNibName:@"ViewController" bundle:nil];
        [view.view setBackgroundColor:[colorArray objectAtIndex:i]];
        view.title = [NSString stringWithFormat: @"%dst", i];
        [self.View addObject:view];
    }
    self.TabBarCV.viewControllers = self.View;
    self.TabBarCV.customizableViewControllers = self.View;
    
    self.window = [[UIWindow alloc] initWithFrame:[UIScreen mainScreen].bounds];
    self.window.rootViewController = self.TabBarCV;
    [self.window makeKeyAndVisible];
    return YES;
}
效果图如下:


创建函数介绍:

上面创建实例的代码中。使用的是下面方法:

self.TabBarCV.viewControllers = self.View;
self.TabBarCV.customizableViewControllers = self.View;

这个方法可以创建多个view,如果view多于5个,那么左边会出现这个more的按钮,点击more,现实多余无法显示的界面。

下面那个方法就是设置允许我们editor(编辑)的view的,在这个customizableViewControllers的View,都是可以在more这个Navigation中编辑的。

同时,我们还可以使用另一种方法来添加view。如下:

[self.TabBarCV addChildViewController:view];

这个方法和上面的方法一样。但是使用这个方法就只能最多添加5个View。

自定义的设置TabBar中的标签item方法介绍:

我们可以自定义的设置TabBar中的标签item。使用下面的方法:

BarController *controller5 = [[BarController alloc] initWithNibName:nil bundle:nil];
controller5.tabBarItem = [[UITabBarItem alloc] initWithTitle:[self.titleArray objectAtIndex:i] image:[UIImage imageNamed:@"Ellipse 1"] selectedImage:[UIImage imageNamed:@"Ellipse 1"]];

当然我们也可以使用方法单独设置这些属性,如下:

[controller2.tabBarItem setTitle:@"test"];
[controller2.tabBarItem setSelectedImage:[UIImage imageNamed:@"Ellipse 1"]];
[controller2.tabBarItem setImage:[UIImage imageNamed:@"Ellipse 1"]];
效果图如下:


在TabBarController的View之间实现跳转的方法的介绍:

[self.tabBarController setSelectedIndex:2];
[self.tabBarController setSelectedViewController:[self.tabBarController.viewControllers objectAtIndex:2]];


TabBarController协议和协议方法介绍

我们在编写TabBarControlerView的时候,我们可以调用协议里面的一些方法,下面是协议里面的方法的介绍:

首先:我们要调用协议里面的方法,我们需要在响应的类里面遵循UITabBarControllerDelegate协议:

然后,我们要将自己的tabBarController和代理关联起来,self.tabBarController.delegate =self;

协议中主要调用的方法有:

//这个方法在我们选中tabBar的时候调用。
-(BOOL) tabBarController:(UITabBarController *)tabBarController shouldSelectViewController:(UIViewController *)viewControlle

//这个方法在点击的时候调用
-(void) tabBarController:(UITabBarController *)tabBarController didSelectViewController:(UIViewController *)viewController

//下面三个方法在点击more左上角的edited的时候调用
-(void) tabBarController:(UITabBarController *)tabBarController willBeginCustomizingViewControllers:(NSArray *)viewControllers
- (void)tabBarController:(UITabBarController *)tabBarController willEndCustomizingViewControllers:(NSArray *)viewControllers changed:(BOOL)changed
-(void) tabBarController:(UITabBarController *)tabBarController didEndCustomizingViewControllers:(NSArray *)viewControllers changed:(BOOL)changed{
//其中,第一个方法在编辑tabBarItem的界面即将弹出的时候调用
//第二个方法在即将结束编辑tabBarItem的时候调用。里面的参数:view controller为tabBar Item 中的View和他们相关的位置;changed显示这个tabBarController中的item的位置有没有改变。
//第三个方法在结束编辑tabBar Item 的时候调用。参数和第二个方法一样。

首先三个方法实现点击下面的这个第三个图片的edit的时候调用的时候调用的。

                          

如何将IOS中的tabbar隐藏

一般我们都是在在push一个view的时候,将tabbar给隐藏掉,因此我们需要将添加下面的代码
self.hidesBottomBarWhenPushed = YES;
[self.navigationController pushViewController:vc animated:YES];

然后,我们需要在push的那个view中添加以下代码:
- (void)viewWillAppear:(BOOL)animated {
    [xxxTabBar setTabBarHidden:YES];
}
- (void)viewWillDisappear:(BOOL)animated {
    [xxxTabBar setTabBarHidden:NO];
}



原文地址:https://www.cnblogs.com/AbeDay/p/5026948.html