[iOS微博项目 - 1.6] - 自定义TabBar

A.自定义TabBar

1.需求

控制TabBar内的item的文本颜色(普通状态、被选中状态要和图标一致)、背景(普通状态、被选中状态均为透明)

重新设置TabBar内的item位置,为下一步在TabBar中部添加“+”按钮做准备

github: https://github.com/hellovoidworld/HVWWeibo

系统默认样式:

  • 选中时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 = 0;
43
44     for (UIView *tabBarButton in self.subviews) {
45         if ([tabBarButton isKindOfClass:NSClassFromString(@"UITabBarButton")]) {
46             // 计算尺寸,预留一个“+”号空间
47             CGFloat width = self.width / (self.items.count + 1);
48             tabBarButton.width = width;
49
50             // 计算位置
51             if (index < (int)(self.items.count / 2)) {
52                 tabBarButton.x = width * index;
53             } else {
54                 tabBarButton.x = width * (index + 1);
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 + 1);
73     CGFloat height = self.height;
74     CGFloat x = (self.items.count / 2) * width;
75     CGFloat y = 0;
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 }

时间: 2024-07-31 16:18:44

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

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

自定义tabbar与按钮的添加 01 - 把系统的tabbar用我们自己的覆盖 LHQTabBar *lhqTabBar = [[LHQTabBar alloc]init]; [self setValue:lhqTabBar forKeyPath:@"tabBar"]; 02 对于系统私有的属性,可以通过运行时或者KVC 03 设置每个tabbar的位置 //2 调整UItabbarBtn的大小 //2.1先定义每个tabbar的宽度 CGFloat tabbarBtnW = self

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

github: https://github.com/hellovoidworld/HVWWeibo A.自定义微博cell基本结构 1.需求 创建自定义cell的雏形 cell包含:内容.工具条 内容包含:原创内容.转发内容 2.思路 使用分层控件,逐层实现 分离model和view model:数据模型.frame模型 view:就是控件本身 frame模型:包含数据模型和子控件frame 根据数据模型来决定子控件是否显示(例如转发内容) cell的view设计雏形: 控件的成员属性层次:

iOS开发项目篇—27自定义UITabBar

iOS开发项目篇—27自定义UITabBar 一.自定义 思路: (1)新建一个继承自UITabBar的类,自定义一个UITabBar (2)用自定义的UITabBar换掉系统的UItabBar(使用了KVC) (3)监听控制器的切换,只要控制器一切换,就调用代理方法强制重新布局子控件(内部会调用layoutSubviews). YYTabBar.m文件代码: 1 // 2 // YYTabBar.m 3 // 4 5 #import "YYTabBar.h" 6 7 @interfa

iOS开发项目篇—28自定义UITextView

iOS开发项目篇—28自定义UITextView 一.简单说明 1.要实现的效果 2.分析 (1)UITextField 1.最多只能输入一行文字 2.能设置提醒文字(placehoder) 3.不具备滚动功能 (2)UITextView 1.能输入N行文字(N>0) 2.不能设置提醒文字(没有placehoder属性) 3.具备滚动功能 需求:技能输入多行文字,又具备文字提醒功能. 这里选择自定义一个类,让其继承自UITextView类,为其添加一个设置文字提醒的功能. 二.实现 自定义UI控

iOS开发项目篇—29自定义工具条

iOS开发项目篇—29自定义工具条 一.简单说明 1.实现效果: 2.实现思路: (1)尝试: 1 //添加子控件 2 -(void)setupTextView 3 { 4 //1.创建输入控件 5 YYTextView *textView=[[YYTextView alloc]init]; 6 //设置frame 7 textView.frame=self.view.bounds; 8 [self.view addSubview:textView]; 9 self.textView=textV

蓝懿ios微博项目之自定义cell

/重写转发微博View的get方法  懒加载,实现了转发微博view复用 // 懒加载就是在程序运行后,不是一下子加载很多的控件,而是后期需要的时候再复用 // 懒加载的形式是重写get方法的同时,里面进行判断跟着 if(obj==nil),这种形式 //  这里的转发的微博view也要考虑view的复用问题 -(LYWeiboView *)reWeiboView{ // if里面判断进行的时候要写成_reWeiboView,如果写self.reWeiboView,它属于_eWeiboView的

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

一.UINavigationItem1> 获得方式self.navigationItem // self是指控制器 2> 作用可以用来设置当前控制器顶部导航栏的内容// 设置导航栏中间的内容self.navigationItem.titleself.navigationItem.titleView 二.UIBarButtonItem1> 用在什么地方// 设置导航栏左上角的内容self.navigationItem.leftBarButtonItem// 设置导航栏右上角的内容self.

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

A.搭建基本环境 github: https://github.com/hellovoidworld/HVWWeibo 项目结构: 1.使用代码构建UI,不使用storyboard AppDelegate: 1 - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions { 2 // Override point for customiza

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

github: https://github.com/hellovoidworld/HVWWeibo   A.获取登陆用户未读消息 1.需求 获取所有未读消息,包括新微博.私信.@.转发.关注等 把未读消息数显示在相应的tabItem上 把总的未读消息数显示在app图标上 当app进入后台,仍然需要刷新未读消息数量数据 读取了未读消息之后清空计数 监听tabBarItem的点击,刷新数据(例如重复点击"首页"要刷新微博) 2.思路 使用微博提醒API获取未读消息 使用定时器定时获取 在