iOS開發之UITabBarController

1、概述數組

跟UINavigationController相似,UITabBarController也能夠輕鬆地管理多個控制器,輕鬆完成控制器之間的切換,典型例子就是QQ、微信等應用。微信

2UITabBarController的簡單使用框架

UITabBarController的使用步驟:atom

(1)初始化UITabBarController索引

(2)設置UIWindow的rootViewController爲UITabBarController圖片

(3)根據具體狀況,經過addChildViewController方法添加對應個數的子控制器it

3UITabBarController的子控制器io

UITabBarController添加控制器的方式有2種:渲染

(1)添加單個子控制器select

- (void)addChildViewController:(UIViewController *)childController;

(2)設置子控制器數組

@property(nonatomic,copy) NSArray *viewControllers;

(3)根據索引選擇子控制器

self.selectedIndex = 0;//選中第0個子控制器

4UITabBar

若是UITabBarController有N個子控制器,那麼UITabBar內部就會有N個UITabBarButton做爲子控件。

例如:若是UITabBarController有4個子控制器,那麼UITabBar的結構大體以下圖所示:

5UITabBarButton

UITabBarButton裏面顯示什麼內容,由對應子控制器的tabBarItem屬性決定:

例如:

UIViewController *vc1 = [[UIViewController alloc] init];

vc1.tabBarItem.title = @"聯繫人";

vc1.tabBarItem.image = [UIImage imageNamed:@"tab_buddy_nor"];

點擊相關tabBarItem後,系統會自動把按鈕變爲上圖所示藍色,並不須要提供藍色的按鈕圖片了。也能夠經過調用selectedImage方法設置點擊後顯示的圖片。

例如:

childVc.tabBarItem.selectedImage =

[[UIImage imageNamed:selectedImageName]

imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];

IOS7必須設置渲染模式爲不渲染,不然選擇後還會變成藍色。IOS6不用設置。但IOS6沒有此方法,因此用此方法前要根據系統版本進行分狀況。例如:

#define iOS7 ([[UIDevice currentDevice].systemVersion doubleValue] >= 7.0)

if (iOS7) {

        childVc.tabBarItem.selectedImage = [selectedImage imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];

    } else {

        childVc.tabBarItem.selectedImage = selectedImage;

    }

也能夠在storyboard中直接拖一個UITabBarControllerView過去,而後右鍵選擇view controllers拖線到子控制器添加子控制器,以下圖:

6App主流UI框架結構

 

七、         Modal

除了push以外,還有另一種控制器的切換方式,那就是Modal。任何控制器都能經過Modal的形式展現出來。Modal的默認效果:新控制器從屏幕的最底部往上鑽,直到蓋住以前的控制器爲止。

Modal的形式展現控制器:

-(void)presentViewController:(UIViewController *)viewControllerToPresent animated: (BOOL)flag completion:(void (^)(void))completion

例如:

MJTwoViewController *two = [[MJTwoViewController alloc] init];

[self presentViewController:two animated:YES completion:^{

   NSLog(@"展現MJTwoViewController完畢.......");//展現完畢會調用

}];

關閉當初Modal出來的控制器:

- (void)dismissViewControllerAnimated: (BOOL)flag completion: (void (^)(void))completion;

例如:

[self dismissViewControllerAnimated:YES completion:^{

       NSLog(@"關閉MJTwoViewController....");//關閉後會執行

}];

相關文章
相關標籤/搜索