(转)自定义UITabBar
2024-10-21 03:42:42
push页面时,可调用hidesBottomBarWhenPushed进行隐藏。
第一步,我们需要一些图片:
各个选项的图标和tabbar的背景图片,最后还要一个透明的1x1像素的图片。
第二步,新建一个工程,在工程内建一个继承于UITabBarController的类。
第三步,首先写一个方法,返回一个UINavigationController
- -(UINavigationController*) viewControllerWithTitle:(NSString*) title image:(UIImage*)image
- {
- UIViewController* viewController = [[UIViewController alloc] init];
- viewController.tabBarItem = [[UITabBarItem alloc] initWithTitle:title image:image tag:0];
- viewController.title = title;
- UINavigationController *nav = [[UINavigationController alloc]initWithRootViewController:viewController];
- return nav;
- }
然后在viewDidLoad里面创建TabbarController的viewControllers
- self.viewControllers = [NSArray arrayWithObjects:
- [self viewControllerWithTitle:@"1" image:IMG(@"1")],
- [self viewControllerWithTitle:@"2" image:IMG(@"2")],
- [self viewControllerWithTitle:nil image:nil],
- [self viewControllerWithTitle:@"3" image:IMG(@"3")],
- [self viewControllerWithTitle:@"4" image:IMG(@"4")], nil nil];
看到没有,比较猥琐的就是第三个ViewController什么都没设置。
因为我们要在那个位置放一个自己的按钮,继续在viewDidLoad写:
- UIButton* button = [[UIButton alloc]initWithFrame:CGRectMake(0.0, 0.0, 65, 65)];
- button.center = CGPointMake(160, 20);
- [button setBackgroundImage:IMG(@"add") forState:UIControlStateNormal];
- [button addTarget:self action:@selector(add:) forControlEvents:UIControlEventTouchUpInside];
- [self.tabBar addSubview:button];
然后设置背景图片:
- [self.tabBar setBackgroundImage:IMG(@"tabbarbg")];
运行之后是这样的:
会发现按钮上面有一条横线,然后再设置这个阴影运行后就没有人发现你猥琐的行径。
- [self.tabBar setShadowImage:IMG(@"transparent")];
最后效果图(iOS7和iOS6):
这样做的好处:
在ViewController里push页面的时候可调用hidesBottomBarWhenPushed的属性进行隐藏。
最新文章
- 【UI插件】简单的日历插件(下)—— 学习MVC思想
- Codeforces Round #389 (Div. 2, Rated, Based on Technocup 2017 - Elimination Round 3) D. Santa Claus and a Palindrome STL
- FFT的分析以及matlab实验
- android 应用笔记
- 一张图让你看懂各开源License[转]
- C# 运行时编辑 节点重命名
- Windows 小技巧: 變更輸入法順序
- 利用 SerialPort 控件实现 PC 串口通信
- 初识CLR
- hdu4707 Pet
- 浅析——SCTP协议(转)
- pptv web前端面试题
- wireshark教程
- Python学习入门基础教程(learning Python)--5.6 Python读文件操作高级
- POJ 1789 Truck History(Prim+邻接矩阵)
- C#单元测试,带你入门
- 【机器学习】彻底搞懂CNN
- hdu 5119(2014北京)
- #一周五# VS2015 CTP6, TFS2015 CTP1更新,老衣的开发工具汇总,2015 MVP 社区巡讲
- June. 25th 2018, Week 26th. Monday