您的位置:首页 > 其它

tabbarItem 怎么不显示?

2016-01-09 23:13 267 查看
今天是第一次写博客。我是一个博客新手,也是一个iOS开发新手。

我希望我自己写的东西,能够帮到像我一样的新手菜鸟。

创建了一个继承UItabbarcontroller的控制器

写完以下代码运行,发现模拟器上只有tabbar的背景图片,对应的Item项怎么没有显示?

// 文字

NSArray * titleArray =
@[@"首页",@"分类",@"购物车",@"我的账户"];

// 选中时的图片

NSArray * selectedArray =
@[@"tabbar_index",@"tabbar_cat",@"tabbar_cart",@"myaccount"];

// 未选中时的图片

NSArray * unSelectedArray =
@[@"tabbar_index",@"tabbar_cat",@"tabbar_cart",@"myaccount"];

for (int i=0; i<titleArray.count; i++) {

// 读取每一项

UITabBarItem * item =
self.tabBar.items[i];

// bar使用图片需要经过处理才可以使用

UIImage * selectedImage = [UIImage
imageNamed:selectedArray[i]];

selectedImage = [selectedImage imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];

UIImage * unSelectedImage = [UIImage
imageNamed:unSelectedArray[i]];

unSelectedImage = [unSelectedImage imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];

item = [item initWithTitle:titleArray[i]
image:unSelectedImage
selectedImage:selectedImage];

// 设置Item选中和未选中的颜色

[item setTitleTextAttributes:@{NSForegroundColorAttributeName:[UIColor
blackColor]}
forState:UIControlStateNormal];

[item setTitleTextAttributes:@{NSForegroundColorAttributeName:[UIColor
whiteColor]}
forState:UIControlStateSelected];

}

// 设置tabbar背景

[self.tabBar
setBackgroundImage:[UIImage
imageNamed:@"tab_bg.jpg"]];

然后我咨询了一下一位正在参加iOS培训班的同学(刚好学到这一块了)。然而他给我的回答让我无法忍受,居然说我这个不就是自定义的tabbar嘛?自定义要把系统的隐藏,我就无语了,心想我这个就是系统的隐藏个毛线啊,然后,我又去百度了,没有搜到我想要的答案,不知道是不是我的搜索的关键词问题。那么言归正传,怎么才能让tabbar上面显示出Item呢?我想,是不是缺少类似容器什么的?果然添加下面的代码就可以了显示出Item了!

// 首页

IndexViewController * vc1 = [[IndexViewController
alloc]initWithNibName:nil
bundle:nil];

UINavigationController * nc1 = [[UINavigationController
alloc]initWithRootViewController:vc1];

// 分类

CategoryViewController * vc2 = [[CategoryViewController
alloc]initWithNibName:nil
bundle:nil];

UINavigationController * nc2 = [[UINavigationController
alloc]initWithRootViewController:vc2];

// 购物车

ShoppingCarViewController * vc3 = [[ShoppingCarViewController
alloc]initWithNibName:nil
bundle:nil];

UINavigationController * nc3 = [[UINavigationController
alloc]initWithRootViewController:vc3];

// 我的账户

MyAccountViewController * vc4 = [[MyAccountViewController
alloc]initWithNibName:nil
bundle:nil];

UINavigationController * nc4 = [[UINavigationController
alloc]initWithRootViewController:vc4];

self.viewControllers =
@[nc1,nc2,nc3,nc4];

最后一句代码也许就是对应的属性。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: