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的字体属性
 - (void)viewDidAppear:(BOOL)animated {
NSMutableDictionary *attr = [NSMutableDictionary dictionary];
attr[NSForegroundColorAttributeName] = [UIColor orangeColor]; for (UITabBarItem *item in self.tabBar.items) {
[item setTitleTextAttributes:attr forState:UIControlStateSelected];
}
}
 
 
#mark: 注意,TabBarItem相当于模型数据,TabBar才是view
 
(2)自定义一个集成UITabBar的类,用来封装定制tabBar
  • 封装上述的改变TabBarButton文本颜色的代码
  • 重写TabBarButton的位置尺寸,中间空出一个位置放置“+”按钮
 
 //
// HVWTabBar.m
// HVWWeibo
//
// Created by hellovoidworld on 15/2/3.
// Copyright (c) 2015年 hellovoidworld. All rights reserved.
// #import "HVWTabBar.h" @implementation HVWTabBar - (void)layoutSubviews {
// 切记一定要调用父类的方法!!!
[super layoutSubviews]; // 设置文本属性
[self initTextAttr]; // 设置BarButton的位置
[self initBarButtonPosition]; // 添加"+"按钮
[self addComposeButton];
} /** 设置文本属性 */
- (void) initTextAttr {
NSMutableDictionary *attr = [NSMutableDictionary dictionary];
attr[NSForegroundColorAttributeName] = [UIColor orangeColor]; for (UITabBarItem *item in self.items) {
// 设置字体颜色
[item setTitleTextAttributes:attr forState:UIControlStateSelected];
}
} /** 设置BarButton的位置 */
- (void) initBarButtonPosition { // 创建一个位置所以,用来定位
int index = ; for (UIView *tabBarButton in self.subviews) {
if ([tabBarButton isKindOfClass:NSClassFromString(@"UITabBarButton")]) {
// 计算尺寸,预留一个“+”号空间
CGFloat width = self.width / (self.items.count + );
tabBarButton.width = width; // 计算位置
if (index < (int)(self.items.count / )) {
tabBarButton.x = width * index;
} else {
tabBarButton.x = width * (index + );
} index++;
}
}
} /** 添加"+"按钮 */
- (void) addComposeButton {
// 初始化按钮
UIButton *plusButton = [UIButton buttonWithType:UIButtonTypeCustom];
[plusButton setBackgroundImage:[UIImage imageWithNamed:@"tabbar_compose_button"] forState:UIControlStateNormal];
[plusButton setBackgroundImage:[UIImage imageWithNamed:@"tabbar_compose_button_highlighted"] forState:UIControlStateHighlighted];
[plusButton setImage:[UIImage imageWithNamed:@"tabbar_compose_icon_add"] forState:UIControlStateNormal];
[plusButton setImage:[UIImage imageWithNamed:@"tabbar_compose_icon_add_highlighted"] forState:UIControlStateHighlighted]; // 设置位置尺寸
CGFloat width = self.width / (self.items.count + );
CGFloat height = self.height;
CGFloat x = (self.items.count / ) * width;
CGFloat y = ;
plusButton.frame = CGRectMake(x, y, width, height); // 添加到tabBar上
[self addSubview:plusButton];
} @end
 
 
 
(3)"+"按钮点击事件
  • 弹出一个新的界面用来写新微博
  • 新建一个目录“compose”专门负责发微博业务
  • 创建一个集成UIViewController的HVWComposeViewController
 
 //
// HVWComposeViewController.m
// HVWWeibo
//
// Created by hellovoidworld on 15/2/3.
// Copyright (c) 2015年 hellovoidworld. All rights reserved.
// #import "HVWComposeViewController.h" @interface HVWComposeViewController () @end @implementation HVWComposeViewController - (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view. // 初始化一些功能按钮
self.view.backgroundColor = [UIColor redColor];
self.title = @"+号弹出控制器"; self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithTitle:@"退出" style:UIBarButtonItemStylePlain target:self action:@selector(dismiss)];
} - (void) dismiss {
[self dismissViewControllerAnimated:YES completion:nil]; } @end
 
 
点击“+”方法:
 //  HVWTabBarViewController.m
#pragma mark - HVWTabBarDelegate
/** “+”按钮点击代理方法 */
- (void)tabBarDidComposeButtonClick:(HVWTabBar *)tabBar {
HVWComposeViewController *composeView = [[HVWComposeViewController alloc] init]; // tabBarController不是由navigationController弹出来的,没有navigationController
// [self.navigationController pushViewController:vc animated:YES];
// HVWLog(@"%@", self.navigationController); // null // 为了使用导航栏,使用NavigationController包装一下
HVWNavigationViewController *nav = [[HVWNavigationViewController alloc] initWithRootViewController:composeView];
// 使用modal方式弹出
[self presentViewController:nav animated:YES completion:nil];
}
 
 
 
 

[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. Android仿iPhone晃动撤销输入功能(微信摇一摇功能)

    重力传感器微信摇一摇SensorMannager自定义alertdialogSensorEventListener 很多程序中我们可能会输入长文本内容,比如短信,写便笺等,如果想一次性撤销所有的键入内 ...

  2. System.exit(0)

    表示程序正常退出 System.exit(status) 当status非0时,表示程序为非正常退出. status=0, 关闭当前正在运行的虚拟机. 求质因数分解的程序如下: 两种算法: 一种是用S ...

  3. pfx 转 snk

    最近用 fody 加在c#工程内,但是签名只认snk ,好像是mono cecil的问题,都不认pfx,重新生成snk文件,publishkey又要变了, 底层dll引用的地方太多,要改好多cspro ...

  4. GET,POST,PUT,DELETE的区别

    Http定义了与服务器交互的不同方法,最基本的方法有4种,分别是GET,POST,PUT,DELETE.URL全称是资源描述符,我们可以这样认为:一个URL地址,它用于描述一个网络上的资源,而HTTP ...

  5. Raphael绘制圆圈环绕方法

    $scope.toRadians = function (degrees) { return degrees * (Math.PI / 180); } $scope.toDegrees = funct ...

  6. C#开发COM+组件和ActiveX控件

    using System.Reflection; using System.Runtime.CompilerServices; using System.Runtime.InteropServices ...

  7. HDU 3844 Mining Your Own Business(割点,经典)

    题意: 给出一个连通图,要求将某些点涂黑,使得无论哪个点(包括相关的边)撤掉后能够成功使得剩下的所有点能够到达任意一个涂黑的点,颜料不多,涂黑的点越少越好,并输出要涂几个点和有多少种涂法. 思路: 要 ...

  8. 22个所见即所得在线 Web 编辑器

    前言: 关于编辑器,适合的才是最好的,接下来,我会写一些关于日志编辑器的文章,今天就写写,可能内容会比较多. --------------------------------------------- ...

  9. [Papers]NSE, $u_3$, Lebesgue space [NNP, QM, 2002; Zhou, JMPA, 2005]

    $$\bex u_3\in L^p(0,T;L^q(\bbR^3)),\quad \frac{2}{p}+\frac{3}{q}=\frac{1}{2},\quad 6< q\leq \inft ...

  10. FOJ2022车站 线段树区间合并

    http://acm.fzu.edu.cn/problem.php?pid=2022 刚开始MLE,用map对应,果断爆内存了,然后改用去重,离散化, lowbound查找元素位置,速度还不错,不过p ...