UITabBarController

1. 开启AppDelegate.h,并加入如下代码

 
 1     #import <UIKit/UIKit.h>  
 2       
 3     @interface AppDelegate : UIResponder <UIApplicationDelegate>  
 4     {  
 5         UITabBarController* tabBarViewController;  
 6     }  
 7       
 8     @property (strong, nonatomic) UIWindow *window;  
 9       
10     @end  

 

2. 开启AppDelegate.m,并加入如下代码

 
 1     - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions  
 2     {  
 3         self.window = [[[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]] autorelease];  
 4         // Override point for customization after application launch.  
 5         self.window.backgroundColor = [UIColor whiteColor];  
 6         [self.window makeKeyAndVisible];  
 7           
 8         tabBarViewController = [[UITabBarController alloc]init];  
 9         [self.window setRootViewController:tabBarViewController];  
10           
11         FirstViewController* first = [[FirstViewController alloc]init];  
12         SecondViewController* second = [[SecondViewController alloc]init];  
13         tabBarViewController.viewControllers = [NSArray arrayWithObjects:first, second, nil];  
14         [first release];  
15         [second release];  
16     }  
1     tabBarViewController = [[UITabBarController alloc]init];  
2     [self.window setRootViewController:tabBarViewController];  

 

第一行代码为初始一个UITabBarController

第二行为将tabBarViewController设定为window的root view controller(根视图控制器)

 

1 tabBarViewController.viewControllers = [NSArray arrayWithObjects:first, second, nil];  

 

 

接下来利用UITabBarController的viewControllers属性,设定First及Second两个控制器。

3.用代码修改TabBar与TabBarItem

1     UITabBar *tabBar = tabBarViewController.tabBar;  
2     UITabBarItem *tabBarItem1 = [tabBar.items objectAtIndex:0];  
3     UITabBarItem *tabBarItem2 = [tabBar.items objectAtIndex:1];  

UITabBarController提供一个tabBar属性,我们可以透过这个属性取得UITabBar

并在UITabBar的items属性取得所有的UITabBarItem

 

1     tabBarItem1.title = @"Home";  
2     tabBarItem2.title = @"Maps";  

 

透过UITabBarItem的title属性,可以设定tab bar item上显示的文字

1     [tabBarItem1 setFinishedSelectedImage:[UIImage imageNamed:@"home_selected.png"] withFinishedUnselectedImage:[UIImage imageNamed:@"home.png"]];  
2     [tabBarItem2 setFinishedSelectedImage:[UIImage imageNamed:@"maps_selected.png"] withFinishedUnselectedImage:[UIImage imageNamed:@"maps.png"]];  

 

- (void)setFinishedSelectedImage:(UIImage *)selectedImage withFinishedUnselectedImage:(UIImage *)unselectedImage

这是UITabBarItem提供的方法,可以设定上面所显示的图片,selectedImage是只目前选择并显示的TabBatItem显示的图片

unselectedImage则是平常未选中时显示的图片

1 UIImage* tabBarBackground = [UIImage imageNamed:@"tabbar.png"];  
2 [[UITabBar appearance] setBackgroundImage:tabBarBackground]; 

这段代码可以修改UITabBar显示的背景图

1 [[UITabBar appearance] setSelectionIndicatorImage:[UIImage imageNamed:@"tabbar_selected.png"]];  

这可以设定选中的UITabBarItem后面的图

1         UIColor *titleHighlightedColor = [UIColor colorWithRed:153/255.0 green:192/255.0 blue:48/255.0 alpha:1.0];  
2         [[UITabBarItem appearance] setTitleTextAttributes:[NSDictionary dictionaryWithObjectsAndKeys:  
3                                                                titleHighlightedColor, UITextAttributeTextColor,  
4                                                                nil] forState:UIControlStateHighlighted]; 
- (void)setTitleTextAttributes:(NSDictionary *)attributes forState:(UIControlState)state

这个方法可以设定显示文字的属性,在这段代码中,是设定为显示文字的颜色

 

posted @ 2015-01-24 17:40  梦中人兰  阅读(130)  评论(0)    收藏  举报