A.自定义TabBar
1.需求
控制TabBar内的item的文本颜色(普通状态、被选中状态要和图标一致)、背景(普通状态、被选中状态均为透明)
重新设置TabBar内的item位置,为下一步在TabBar中部添加“+”按钮做准备
 
系统默认样式:
  • 选中时item字体颜色为蓝色
 
 
完成之后的样式:
 
 
2.思路
封装TabBar,继承UITabBar,重写有关TabBar内部控件的位置设置方法
使用KVC重设UITabBarController中的tabBar成员(因为UITabBarController中的tabBar是只读的,KVC能够直接修改_tabBar)
修改UITabBar的item属性,修改字体(普通、被选中状态)
 
3.实现探索
(1)修改item的字体颜色
设置UITabBar的代理,监听item选中事件,更改item的字体属性
  1. - (void)viewDidAppear:(BOOL)animated {
  2. NSMutableDictionary *attr = [NSMutableDictionary dictionary];
  3. attr[NSForegroundColorAttributeName] = [UIColor orangeColor];
  4.  
  5. for (UITabBarItem *item in self.tabBar.items) {
  6. [item setTitleTextAttributes:attr forState:UIControlStateSelected];
  7. }
  8. }
 
 
#mark: 注意,TabBarItem相当于模型数据,TabBar才是view
 
(2)自定义一个集成UITabBar的类,用来封装定制tabBar
  • 封装上述的改变TabBarButton文本颜色的代码
  • 重写TabBarButton的位置尺寸,中间空出一个位置放置“+”按钮
 
  1. //
  2. // HVWTabBar.m
  3. // HVWWeibo
  4. //
  5. // Created by hellovoidworld on 15/2/3.
  6. // Copyright (c) 2015年 hellovoidworld. All rights reserved.
  7. //
  8.  
  9. #import "HVWTabBar.h"
  10.  
  11. @implementation HVWTabBar
  12.  
  13. - (void)layoutSubviews {
  14. // 切记一定要调用父类的方法!!!
  15. [super layoutSubviews];
  16.  
  17. // 设置文本属性
  18. [self initTextAttr];
  19.  
  20. // 设置BarButton的位置
  21. [self initBarButtonPosition];
  22.  
  23. // 添加"+"按钮
  24. [self addComposeButton];
  25. }
  26.  
  27. /** 设置文本属性 */
  28. - (void) initTextAttr {
  29. NSMutableDictionary *attr = [NSMutableDictionary dictionary];
  30. attr[NSForegroundColorAttributeName] = [UIColor orangeColor];
  31.  
  32. for (UITabBarItem *item in self.items) {
  33. // 设置字体颜色
  34. [item setTitleTextAttributes:attr forState:UIControlStateSelected];
  35. }
  36. }
  37.  
  38. /** 设置BarButton的位置 */
  39. - (void) initBarButtonPosition {
  40.  
  41. // 创建一个位置所以,用来定位
  42. int index = ;
  43.  
  44. for (UIView *tabBarButton in self.subviews) {
  45. if ([tabBarButton isKindOfClass:NSClassFromString(@"UITabBarButton")]) {
  46. // 计算尺寸,预留一个“+”号空间
  47. CGFloat width = self.width / (self.items.count + );
  48. tabBarButton.width = width;
  49.  
  50. // 计算位置
  51. if (index < (int)(self.items.count / )) {
  52. tabBarButton.x = width * index;
  53. } else {
  54. tabBarButton.x = width * (index + );
  55. }
  56.  
  57. index++;
  58. }
  59. }
  60. }
  61.  
  62. /** 添加"+"按钮 */
  63. - (void) addComposeButton {
  64. // 初始化按钮
  65. UIButton *plusButton = [UIButton buttonWithType:UIButtonTypeCustom];
  66. [plusButton setBackgroundImage:[UIImage imageWithNamed:@"tabbar_compose_button"] forState:UIControlStateNormal];
  67. [plusButton setBackgroundImage:[UIImage imageWithNamed:@"tabbar_compose_button_highlighted"] forState:UIControlStateHighlighted];
  68. [plusButton setImage:[UIImage imageWithNamed:@"tabbar_compose_icon_add"] forState:UIControlStateNormal];
  69. [plusButton setImage:[UIImage imageWithNamed:@"tabbar_compose_icon_add_highlighted"] forState:UIControlStateHighlighted];
  70.  
  71. // 设置位置尺寸
  72. CGFloat width = self.width / (self.items.count + );
  73. CGFloat height = self.height;
  74. CGFloat x = (self.items.count / ) * width;
  75. CGFloat y = ;
  76. plusButton.frame = CGRectMake(x, y, width, height);
  77.  
  78. // 添加到tabBar上
  79. [self addSubview:plusButton];
  80. }
  81.  
  82. @end
 
 
 
(3)"+"按钮点击事件
  • 弹出一个新的界面用来写新微博
  • 新建一个目录“compose”专门负责发微博业务
  • 创建一个集成UIViewController的HVWComposeViewController
 
  1. //
  2. // HVWComposeViewController.m
  3. // HVWWeibo
  4. //
  5. // Created by hellovoidworld on 15/2/3.
  6. // Copyright (c) 2015年 hellovoidworld. All rights reserved.
  7. //
  8.  
  9. #import "HVWComposeViewController.h"
  10.  
  11. @interface HVWComposeViewController ()
  12.  
  13. @end
  14.  
  15. @implementation HVWComposeViewController
  16.  
  17. - (void)viewDidLoad {
  18. [super viewDidLoad];
  19. // Do any additional setup after loading the view.
  20.  
  21. // 初始化一些功能按钮
  22. self.view.backgroundColor = [UIColor redColor];
  23. self.title = @"+号弹出控制器";
  24.  
  25. self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithTitle:@"退出" style:UIBarButtonItemStylePlain target:self action:@selector(dismiss)];
  26. }
  27.  
  28. - (void) dismiss {
  29. [self dismissViewControllerAnimated:YES completion:nil];
  30.  
  31. }
  32.  
  33. @end
 
 
点击“+”方法:
  1. // HVWTabBarViewController.m
  2. #pragma mark - HVWTabBarDelegate
  3. /** “+”按钮点击代理方法 */
  4. - (void)tabBarDidComposeButtonClick:(HVWTabBar *)tabBar {
  5. HVWComposeViewController *composeView = [[HVWComposeViewController alloc] init];
  6.  
  7. // tabBarController不是由navigationController弹出来的,没有navigationController
  8. // [self.navigationController pushViewController:vc animated:YES];
  9. // HVWLog(@"%@", self.navigationController); // null
  10.  
  11. // 为了使用导航栏,使用NavigationController包装一下
  12. HVWNavigationViewController *nav = [[HVWNavigationViewController alloc] initWithRootViewController:composeView];
  13. // 使用modal方式弹出
  14. [self presentViewController:nav animated:YES completion:nil];
  15. }
 
 
 
 

[iOS微博项目 - 1.6] - 自定义TabBar的更多相关文章

  1. iOS开发项目之四 [ 调整自定义tabbar的位置与加号按钮的位置]

    自定义tabbar与按钮的添加 01 - 把系统的tabbar用我们自己的覆盖 LHQTabBar *lhqTabBar = [[LHQTabBar alloc]init]; [self setVal ...

  2. [iOS微博项目 - 4.0] - 自定义微博cell

    github: https://github.com/hellovoidworld/HVWWeibo A.自定义微博cell基本结构 1.需求 创建自定义cell的雏形 cell包含:内容.工具条 内 ...

  3. [iOS微博项目 - 1.4] - 各种item NavigationBar & NavigationItem & BarButtonItem || TabBar & TabBarItem

    一.UINavigationItem1> 获得方式self.navigationItem // self是指控制器2> 作用可以用来设置当前控制器顶部导航栏的内容// 设置导航栏中间的内容 ...

  4. [iOS微博项目 - 1.0] - 搭建基本框架

    A.搭建基本环境   github: https://github.com/hellovoidworld/HVWWeibo   项目结构:   1.使用代码构建UI,不使用storyboard     ...

  5. [iOS微博项目 - 3.6] - 获取未读消息

    github: https://github.com/hellovoidworld/HVWWeibo   A.获取登陆用户未读消息 1.需求 获取所有未读消息,包括新微博.私信.@.转发.关注等 把未 ...

  6. [iOS微博项目 - 3.1] - 发微博界面

    github: https://github.com/hellovoidworld/HVWWeibo   A.发微博界面:自定义UITextView 1.需求 用UITextView做一个编写微博的输 ...

  7. [iOS微博项目 - 3.0] - 手动刷新微博

    github: https://github.com/hellovoidworld/HVWWeibo   A.下拉刷新微博 1.需求 在“首页”界面,下拉到一定距离的时候刷新微博数据 刷新数据的时候使 ...

  8. [iOS微博项目 - 1.7] - 版本新特性

    A.版本新特性 1.需求 第一次使用新版本的时候,不直接进入app,而是展示新特性界面 github: https://github.com/hellovoidworld/HVWWeibo       ...

  9. [iOS微博项目 - 1.1] - 设置导航栏主题(统一样式)

    A.导航栏两侧文字按钮 1.需求: 所有导航栏两侧的文字式按钮统一样式 普通样式:橙色 高亮样式:红色 不可用样式:亮灰 阴影:不使用 字体大小:15   github: https://github ...

随机推荐

  1. Effective C++学习笔记 条款06:如不想使用编译器自动生成的函数,就该明确拒绝

    一.为驳回编译器自动提供的机能,可将相应成员函数声明为private并且不予实现.(如果你仅仅是自己不实现的话,编译器会帮你实现) 如: class A { public: A(const strin ...

  2. iOS开发:插件记录

    进入沙盒的插件 https://github.com/TongeJie/ZLGotoSandboxPlugin 图片提示的插件 https://github.com/ksuther/KSImageNa ...

  3. CodeForces Round #278 (Div.2) (待续)

    A 这么简单的题直接贴代码好了. #include <cstdio> #include <cmath> using namespace std; bool islucky(in ...

  4. Asp.net正则获取html内容

    1.获取div内容 string str = "tt<u>ss</u><div id=\"test\"><div>< ...

  5. Android基础_1 四大基本组件介绍与生命周期

    Android四大基本组件分别是Activity,Service(服务),Content Provider(内容提供者),BroadcastReceiver(广播接收器). 一.四大基本组件 Acti ...

  6. Android Broadcast Receiver

    说明 有时候我们在做android系统软件的时候,经常会需要做的事就是开机重新设置上次关机前的状态,当然,我们就会用到这个开机广播: <uses-permission android:name= ...

  7. PHP单元测试工具PHPUnit初体验

    今天接到了个任务,需要对数字进行计算,因为涉及到整数,小数,和科学计数法等很多条件,所以人工测试非常麻烦,于是想到了PHP的单元测试工具PHPUnit,所以写个文档备查. 看了PHPUnit的文档之后 ...

  8. jquery插件——日历控件

    今天在网上有看到一个jquery插件——日历控件,不过之前也在柯乐义的网站上看到了(http://keleyi.com/ 推荐下) 这个插件看着比较大气,所以干脆也分享下,以后自己也好用一点儿 1.页 ...

  9. solr4.2 solrconfig.xml配置文件简单介绍

    对于solr4.x的每个core有两个很重要的配置文件:solrconfig.xml和schema.xml,下面我们来了解solrconfig.xml配置文件. 具体很详细的内容请细读solrcofi ...

  10. [HTML Q&A][转]使pre的内容自动换行

    <pre> 元素可定义预格式化的文本.被包围在 pre 元素中的文本通常会保留空格和换行符.而文本也会呈现为等宽字体. <pre> 标签的一个常见应用就是用来表示计算机的源代码 ...