02百思不得姐设置TabBar

来源:互联网 发布:怎么看淘宝卖家的佣金 编辑:程序博客网 时间:2024/04/30 00:50


首先我们来分析下界面:

这是一典型的TabBarController,底部TabBar所拥有的元素有图片和文字,并且可以看到这个图片和文字有两种状态一种是默认灰色的,和一种点击状态的图片,
好了现在界面元素已经分析完了接下来就开始代码编写了!!!!!!

1>>首先我们删除项目中无用文件并且新建我们自己的TabBarController

2>>既然我们已经有了这个TabBarController那我们就把它设置成根控制器那么我们在AppDelegate设置下

    /*当程序启动执行完毕时会调用此方法/ 
- (BOOL)application:(UIApplication )application didFinishLaunchingWithOptions:(NSDictionary )launchOptions {
// Override point for customization after application launch.
//创建窗口
self.window = [[UIWindow alloc]init];
self.window.frame = [UIScreen mainScreen].bounds;
//设置窗口的根控制器
self.window.rootViewController = [[GCSTabBarController alloc]init];
//显示窗口
[self.window makeKeyAndVisible];
return YES;
}
NOTE:
这里需要注意的就是当你实例化一个window窗口是一定要设置他的大小否则是不会显示的并且设置为可显示状态,最好试验下哈!!!!绝知此事要躬行

3>>既然这个根控制器已经设置好了,那该里面的东西了也!我们回到自己的TabBarController

- (void)viewDidLoad {    [super viewDidLoad];    // Do any additional setup after loading the view.    NSMutableDictionary *attrs = [NSMutableDictionary dictionary];    attrs[NSFontAttributeName] = [UIFont systemFontOfSize:12];    attrs[NSForegroundColorAttributeName] = [UIColor grayColor];    NSMutableDictionary *selectedAttrs = [NSMutableDictionary dictionary];    selectedAttrs[NSFontAttributeName] = [UIFont systemFontOfSize:12];    selectedAttrs[NSForegroundColorAttributeName] = [UIColor darkGrayColor];    //添加子控制器    UIViewController *vc01 = [[UIViewController alloc]init];    vc01.tabBarItem.title = @"精华";    vc01.tabBarItem.image = [UIImage imageNamed:@"tabBar_essence_icon"];    vc01.tabBarItem.selectedImage = [UIImage imageNamed:@"tabBar_essence_click_icon"];    vc01.view.backgroundColor = [UIColor redColor];    [vc01.tabBarItem setTitleTextAttributes:attrs forState:UIControlStateNormal];    [vc01.tabBarItem setTitleTextAttributes:selectedAttrs forState:UIControlStateSelected];    [self addChildViewController:vc01];    UIViewController *vc02 = [[UIViewController alloc]init];    vc02.tabBarItem.title=@"新帖";    vc02.tabBarItem.image = [UIImage imageNamed:@"tabBar_new_icon"];    vc02.tabBarItem.selectedImage = [UIImage imageNamed:@"tabBar_new_click_icon"];    vc02.view.backgroundColor = [UIColor blackColor];    [vc02.tabBarItem setTitleTextAttributes:attrs forState:UIControlStateNormal];    [vc02.tabBarItem setTitleTextAttributes:selectedAttrs forState:UIControlStateSelected];    [self addChildViewController:vc02];    UIViewController *vc03 = [[UIViewController alloc]init];    vc03.tabBarItem.title = @"关注";    vc03.tabBarItem.image = [UIImage imageNamed:@"tabBar_friendTrends_icon"];    vc03.tabBarItem.selectedImage = [UIImage imageNamed:@"tabBar_friendTrends_click_icon"];    vc03.view.backgroundColor = [UIColor yellowColor];    [vc03.tabBarItem setTitleTextAttributes:attrs forState:UIControlStateNormal];    [vc03.tabBarItem setTitleTextAttributes:selectedAttrs forState:UIControlStateSelected];    [self addChildViewController:vc03];    UIViewController *vc04 = [[UIViewController alloc]init];    vc04.tabBarItem.title = @"我";    vc04.tabBarItem.image = [UIImage imageNamed:@"tabBar_me_icon"];    vc04.tabBarItem.selectedImage = [UIImage imageNamed:@"tabBar_me_click_icon"];    vc04.view.backgroundColor = [UIColor greenColor];    //设置字体的默认和点击两种状态的颜色    [vc04.tabBarItem setTitleTextAttributes:attrs forState:UIControlStateNormal];//    [vc04.tabBarItem setTitleTextAttributes:attrs forState:UIControlStateSelected];    [self addChildViewController:vc04];}

这个方法是在视图加载完之后调用的!我们向TabBarController添加四个自控制器,就像之前我们说的一样这个底部导航有两种状态一种是默认的状态一种是点击后的状态,我们可以看到字体设置是通过字典去给“装饰”的

代码下载地址 http://download.csdn.net/detail/u014360817/9459812

0 0
原创粉丝点击