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];
最后一句代码也许就是对应的属性。
我希望我自己写的东西,能够帮到像我一样的新手菜鸟。
创建了一个继承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];
最后一句代码也许就是对应的属性。
相关文章推荐
- 动态代理模式(抽象角色用接口形式,代理角色必须实现InvocationHandler)
- [Python标准库]operator——内置操作符的函数接口
- uva 11536 - Smallest Sub-Array
- Python读写文件实际操作的五大步骤
- 引用于指针的区别
- 1076. Forwards on Weibo (30)【树+搜索】——PAT (Advanced Level) Practise
- mac学习笔记之:使用brew安装软件
- ASP.NET服务器控件的生命周期分析
- angularjs[ngRepeat:dupes]
- 【HTML/XML 4】实例分析HTML和XML的不同
- hdu1171
- 【HTML/XML 4】实例分析HTML和XML的不同
- Web Service学习笔记(webservice、soap、wsdl、jws详细分析)
- 24种设计模式
- ASP.NET服务器端控件原理分析
- C# 扩展方法
- redis第一天
- Codeforces 615D Multipliers (Round #338 (Div. 2) D题)
- js常用正则表达式
- conda