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
總結
以上是生活随笔為你收集整理的TabBarController创建及使用方法简介的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: Appscan应用
- 下一篇: [Win 7]Windows7 RC 简