02百思不得姐设置TabBar


首先我们来分析下界面:

这是一典型的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
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值