设置UITabBarController的用法

来源:互联网 发布:淘宝能不能卖保健品 编辑:程序博客网 时间:2024/04/30 20:11


   设置UITabBarController的用法的一个示例


#import "TabBarCtrl.h"#import "HomeCtrl.h"#import "TrainCtrl.h"#import "WeatherCtrl.h"@interface TabBarCtrl ()@end@implementation TabBarCtrl- (void)viewDidLoad {    [super viewDidLoad];        //设置控制器的文字和图片    UIStoryboard *homeStory = [UIStoryboard storyboardWithName:@"HomeCtrl" bundle:nil];    //装载Storyboard中的ViewController    HomeCtrl *homeCtrl = [homeStory instantiateViewControllerWithIdentifier:@"HomeCtrlId"];    [self addChildCtrl:homeCtrl title:@"站点查询" image:@"tabbar_home.png" selectedImage:@"tabbar_home_selected.png"];        UIStoryboard *trainStory = [UIStoryboard storyboardWithName:@"TrainCtrl" bundle:nil];    TrainCtrl *trainCtrl = [trainStory instantiateViewControllerWithIdentifier:@"TrainCtrlId"];    [self addChildCtrl:trainCtrl title:@"车次查询" image:@"tabbar_query.png" selectedImage:@"tabbar_query_selected.png"];        UIStoryboard *weatherStory = [UIStoryboard storyboardWithName:@"WeatherCtrl" bundle:nil];    WeatherCtrl *weatherCtrl = [weatherStory instantiateViewControllerWithIdentifier:@"WeatherCtrlId"];    [self addChildCtrl:weatherCtrl title:@"天气查询" image:@"1.gif" selectedImage:@"1.gif"];    }- (void) addChildCtrl:(UIViewController *)childCtrl title:(NSString *) title image:(NSString *) image selectedImage:(NSString *) selectedImage{    //设置控制器的文字和图片    childCtrl.title = title;    //    childCtrl.tabBarItem.title = title;    //    childCtrl.navigationItem.title = title;    childCtrl.tabBarItem.image = [UIImage imageNamed:image];    childCtrl.tabBarItem.selectedImage = [[UIImage imageNamed:selectedImage] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];    //设置文字的样式    NSMutableDictionary *SelectedTextAttrs = [NSMutableDictionary dictionary];        //下面的这种用法相当于array[下标],设置<span style="font-family: Menlo;">NSForegroundColorAttributeName的值</span>    SelectedTextAttrs [NSForegroundColorAttributeName] = [UIColor orangeColor];        [childCtrl.tabBarItem setTitleTextAttributes:SelectedTextAttrs forState:UIControlStateSelected];        UINavigationController *navigation = [[UINavigationController alloc] initWithRootViewController:childCtrl];        [self addChildViewController:navigation];    }@end


  效果图如下:



1 0