设置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效果图如下:
本文介绍了一个使用UITabBarController的示例,展示了如何通过Storyboard加载不同的ViewController,并为每个选项卡设置标题、图标及选中状态下的样式。

1万+

被折叠的 条评论
为什么被折叠?



