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. bzoj1876: [SDOI2009]SuperGCD

    更相减损数. 上手就debug了3个小时,直接给我看哭了. 3个函数都写错了是什么感受? 乘2函数要从前往后乘,这样后面的数乘2进位以后不会干扰前面的数. 除2函数要从后往前除,这样前面的数借来的位不 ...

  2. UVa 10883 (组合数 对数) Supermean

    在纸上演算一下就能看出答案是:sum{ C(n-1, i) * a[i] / 2^(n-1) | 0 ≤ i ≤ n-1 } 组合数可以通过递推计算:C(n, k) = C(n, k-1) * (n- ...

  3. textview设置字体的行距和字间距

    字间距 textView有一个属性android:textScaleX是调节字间距的,它的值是一个float型.查看源代码,默认textView 此属性是使用的是: android.internal. ...

  4. qtcreator cannot find -lts

    /********************************************************************* * qtcreator cannot find -lts ...

  5. apache多站点配置+多端口配置

    Apache多站点配置 这种方法是配置不同的地址访问不同的文件夹的配置 1:修改本机的hosts文件,如下: 示例: 127.0.0.1 localhost 127.0.0.1 www.dede.co ...

  6. 转《深入理解Java虚拟机》学习笔记之最后总结

    编译器 Java是编译型语言,按照编译的时期不同,编译器可分为: 前端编译器:其实叫编译器的前端更合适些,它把*.java文件转变成*.class文件,如Sun的Javac.Eclipse JDT中的 ...

  7. SSH整合所需jar

    Struts2.1.8+Hibernate3.2+Spring4.1.6+MySql ant-1.6.5.jar ant-antlr-1.6.5.jar ant-junit-1.6.5.jar ant ...

  8. Log4NET简介

    log4net库是Apache log4j框架在Microsoft .NET平台的实现,是一个帮助程序员将日志信息输出到各种目标(控制台.文件.数据库等)的工具. 前提 最近做项目需要记录系统日志和用 ...

  9. adb remount 失败remount failed: Operation not permitted

    1. 进入shell adb shell 2. shell下输入命令 shell@android:/ $ sushell@android:/ # mount -o rw,remount -t yaff ...

  10. FTP文件上传与下载

    实现FTP文件上传与下载可以通过以下两种种方式实现(不知道还有没有其他方式),分别为:1.通过JDK自带的API实现:2.通过Apache提供的API是实现. 第一种方式:使用jdk中的ftpClie ...