標籤:
我們在開發中經常會使用到UITabBarController來布局App應用,使用UITabBarController可以使應用看起來更加的清晰,iOS系統的鬧鐘程式,ipod程式都是非常好的說明和Android的底部導航非常相似,最出名的這種布局莫過於。UITabBarController能適用於主線清晰,功能明確的情況,一目瞭然,這樣App才能將想要展示的資料或者說自己公司的產品情懷更好的服務與使用者,關於UITabBar的層次圖,可以參考官網給出圖片:
頁面配置
講解知識點最好的方法就是實戰,先看下可以實現的效果:
底部的一排導航就是TabBar,旅行,書籤,訊息,時鐘都屬於TabBarItem,每個TabBarItem可以設定文字和圖片,寬度是均分的,高度固定為49,最多可以放置五個Item,一般情況為了美觀放置四個,如果超過五個則會多了一個更多的顯示條。
超過五個的效果:
Demo實現
iOS效果實現一般有兩種,一種是直接是StoryBoard中直接拖入一個控制項,然後通過代碼設定,另外一種直接是純程式碼設定,兩種方式看自己個人的喜好,UITabBarController一般都是程式的首頁面,如果是純程式碼設定可以在AppDelegate中的didFinishLaunchingWithOptions實現。
AppDelegate中代碼如下:
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions { // Override point for customization after application launch. self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]]; self.window.backgroundColor = [UIColor whiteColor]; UITabBarController *tabBarController=[[UITabBarController alloc]init]; AirPlaneViewController *planeViewController=[[AirPlaneViewController alloc]init]; [email protected]"旅行"; planeViewController.tabBarItem.image=[UIImage imageNamed:@"Airplane"]; BookmarkViewController *bookmarkController=[[BookmarkViewController alloc]init]; [email protected]"書籤"; bookmarkController.tabBarItem.image=[UIImage imageNamed:@"Bookmark"]; ChatViewController *chatViewController=[[ChatViewController alloc]init]; [email protected]"訊息"; chatViewController.tabBarItem.image=[UIImage imageNamed:@"Chat"]; ClockViewController *clockViewController=[[ClockViewController alloc]init]; [email protected]"時鐘"; clockViewController.tabBarItem.image=[UIImage imageNamed:@"Clock"]; [email protected]"25"; UIViewController *briefController=[[UIViewController alloc]init]; [email protected]"錢包"; briefController.tabBarItem.image=[UIImage imageNamed:@"Breifcase"]; // UIViewController *chestViewController=[[UIViewController alloc]init];// [email protected]"箱子";// chestViewController.tabBarItem.image=[UIImage imageNamed:@"Breifcase"]; NSArray *arrControllers=[NSArray arrayWithObjects:planeViewController,bookmarkController,chatViewController,clockViewController,nil]; tabBarController.viewControllers=arrControllers; //設定根控制器 self.window.rootViewController=tabBarController; //設定Window為主表單 [self.window makeKeyAndVisible]; return YES;}
AirPlaneViewController中的代碼:
- (void)viewDidLoad { [super viewDidLoad]; // Do any additional setup after loading the view. UILabel *label=[[UILabel alloc]initWithFrame:CGRectMake(0, 10, 400, 200)]; [email protected]"部落格園:FlyElephant\n部落格地址:\nhttp://www.cnblogs.com/xiaofeixiang"; label.numberOfLines=0; [self.view setBackgroundColor:[UIColor cyanColor]]; [self.view addSubview:label];}
關於UITabBarController中代碼的設定官網給了邏輯很清晰的圖片,簡單易懂:
UITabController中的viewControllers是一個數組集合,只需要將對應的資料設定給UITabBarController的屬性即可,其他的就是操作每個ViewController,設定每個ViewController的頁面配置和設定,關於拖入控制項的方式,只需要拖入一個TabBarController即可,效果如下,如果設定對應的子控制器即可:
關於Demo的最終效果示範圖:
UITabBarController預設只支援豎屏,當裝置方向放生變化時候,它會查詢viewControllers中包含的所有ViewController,僅當所有的viewController都支援該方向時,UITabBarController才會發生旋轉,否則預設的豎向。當UITabBarController支援旋轉,而且發生旋轉的時候,只有當前顯示的viewController會接收到旋轉的訊息。
iOS開發-UITabBarController詳解