[iOS微博项目 - 1.6] - 自定义TabBar
2015-02-03 15:49
162 查看
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的字体属性
#mark: 注意,TabBarItem相当于模型数据,TabBar才是view
(2)自定义一个集成UITabBar的类,用来封装定制tabBar
封装上述的改变TabBarButton文本颜色的代码
重写TabBarButton的位置尺寸,中间空出一个位置放置“+”按钮
(3)"+"按钮点击事件
弹出一个新的界面用来写新微博
新建一个目录“compose”专门负责发微博业务
创建一个集成UIViewController的HVWComposeViewController
点击“+”方法:
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的字体属性
- (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 = 0; for (UIView *tabBarButton in self.subviews) { if ([tabBarButton isKindOfClass:NSClassFromString(@"UITabBarButton")]) { // 计算尺寸,预留一个“+”号空间 CGFloat width = self.width / (self.items.count + 1); tabBarButton.width = width; // 计算位置 if (index < (int)(self.items.count / 2)) { tabBarButton.x = width * index; } else { tabBarButton.x = width * (index + 1); } 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 + 1); CGFloat height = self.height; CGFloat x = (self.items.count / 2) * width; CGFloat y = 0; 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 之微博项目实战(4)微博自定义tabBar中间的添加按钮
- AJ学IOS 之微博项目实战(4)微博自定义tabBar中间的添加按钮
- 献给初学iOS的小盆友们——微博app项目开发之二自定义tabBar
- [iOS微博项目 - 1.4] - 各种item NavigationBar & NavigationItem & BarButtonItem || TabBar & TabBarItem
- IOS博客项目搭建-15-发微博-自定义键盘上的Toolbar
- 猫猫学iOS 之微博项目实战(5)微博自定义搜索框searchBar
- IOS博客项目搭建-06-自定义TabBarButton-02
- iOS开发项目之四 [ 调整自定义tabbar的位置与加号按钮的位置]
- 蓝懿ios微博项目之自定义cell
- 猫猫学iOS 之微博项目实战(12)发送微博自定义工具条代理实现点击事件
- IOS博客项目搭建-06-自定义TabBar
- AJ学IOS 之微博项目实战(11)发送微博自定义TextView实现带占位文字
- iOS框架搭建(MVC,自定义TabBar)--微博搭建为例
- [iOS微博项目 - 4.0] - 自定义微博cell
- AJ学IOS 之微博项目实战(2)微博主框架-自定义导航控制器NavigationController
- iOS彩票项目--第一天,自定义TabBar控制器和自定义TabBar,自定义导航控制器
- 献给初学iOS的小盆友们——微博app项目开发之十一自定义cell和MVVM模式
- IOS博客项目搭建-14-发微博自定义TextView输入框
- IOS博客项目搭建-06-自定义TabBarButton-03
- AJ学IOS 之微博项目实战(12)发送微博自定义工具条代理实现点击事件