標籤:
一、UITabBarController 使用詳解
UITabBarController是IOS中很常用的一個viewController,例如系統的鬧鐘程式,ipod程式等。UITabBarController通常作為整個程式的rootViewController,而且不能添加到別的container viewController中。
首先我們看一下它的view層級圖:
二、手動建立UITabBarController
最常見的建立UITabBarController的地方就是在application delegate中的 applicationDidFinishLaunching:方法,因為UITabBarController通常是作為整個程式的rootViewController的,我們需要在程式的window顯示之前就建立好它,具體步驟如下:
1、初始化UITabBarController
//初始化UITabBarController UITabBarController *tabbarCon=[[UITabBarController alloc] init];
2、建立tabbarController中每一個tab對應的要顯示的對象,就是子視圖
HHFirstViewController *first=[[HHFirstViewController alloc] initWithNibName:@"HHFirstViewController" bundle:nil]; UINavigationController *firstNav=[[UINavigationController alloc] initWithRootViewController:first];
3、將tab對應的對象添加到UITabBarController中
//將對象添加到tabbarcon中 [tabbarCon setViewControllers:@[firstNav,secondNav,threeNav]];
4、通過設定UITabBarController對象為window.rootViewController,然後顯示window
self.window.rootViewController=tabbarCon; [self.window makeKeyAndVisible];
根據以上4個步驟,UITabBarController的建立基本完全,但是不能滿足我們的正常中的開發,還需要涉及到一些基本的屬性。
三、UITabBarController基本屬性
1、設定tabbar的標題title.
@property(nonatomic,copy) NSString *title; // default is nil
2、設定常量圖片
@property(nonatomic,retain) UIImage *image; // default is nil
3、設定高量圖片
@property(nonatomic,retain) UIImage *selectedImage NS_AVAILABLE_IOS(7_0);
4、設定選中tabbar的字型顏色
/* selectedImageTintColor will be applied to the gradient image used when creating the selected image. Default is nil and will result in the system bright blue for selected tab item images. If you wish to also customize the unselected image appearance, you must use the image and selectedImage properties on UITabBarItem along with UIImageRenderingModeAlways Deprecated in iOS 8.0. On iOS 7.0 and later the selected image takes its color from the inherited tintColor of the UITabBar, which may be set separately if necessary. */@property(nonatomic,retain) UIColor *selectedImageTintColor NS_DEPRECATED_IOS(5_0,8_0,"Use tintColor") UI_APPEARANCE_SELECTOR;
完成這4個屬性,我們的UITabBarController就能完全基本的功能。如:
下面就是建立UITabBarController的全部代碼:
self.window=[[UIWindow alloc]initWithFrame:[[UIScreen mainScreen] bounds]]; HHFirstViewController *first=[[HHFirstViewController alloc] initWithNibName:@"HHFirstViewController" bundle:nil]; UINavigationController *firstNav=[[UINavigationController alloc] initWithRootViewController:first]; HHSecondViewController *second=[[HHSecondViewController alloc] initWithNibName:@"HHSecondViewController" bundle:nil]; UINavigationController *secondNav=[[UINavigationController alloc]initWithRootViewController:second]; HHThreeViewController *three=[[HHThreeViewController alloc] initWithNibName:@"HHThreeViewController" bundle:nil]; UINavigationController *threeNav=[[UINavigationController alloc] initWithRootViewController:three]; //初始化UITabBarController UITabBarController *tabbarCon=[[UITabBarController alloc] init]; //將對象添加到tabbarcon中 [tabbarCon setViewControllers:@[firstNav,secondNav,threeNav]]; tabbarCon.tabBar.selectedImageTintColor=[UIColor orangeColor];//設定選中的字型顏色 UITabBar *tabBar=tabbarCon.tabBar; UITabBarItem *itemFirst=[tabBar.items objectAtIndex:0]; itemFirst.title=@"優貸通"; UITabBarItem *itemSecond=[tabBar.items objectAtIndex:1]; itemSecond.title=@"個人中心"; UITabBarItem *itemThree=[tabBar.items objectAtIndex:2]; itemThree.title=@"更多"; //設定常量圖片 itemFirst.image=[[UIImage imageNamed:@"商戶_normol.png"] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal]; //設定選中的圖片 itemFirst.selectedImage=[[UIImage imageNamed:@"商戶_press.png"] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal]; itemSecond.image=[[UIImage imageNamed:@"我的_normol.png"] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal]; itemSecond.selectedImage=[[UIImage imageNamed:@"我的_press"] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal]; itemThree.image=[[UIImage imageNamed:@"更多_normol.png"] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal]; itemThree.selectedImage=[[UIImage imageNamed:@"更多_press.png"] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal]; self.window.rootViewController=tabbarCon; [self.window makeKeyAndVisible];
View Code
說明:UITabBarButton在UITabBar中得位置是均分的,UITabBar的高度為49。表徵圖的大小為30,或者等比例。
iOS UITabBarController的簡單使用