正常的选项卡流程

cocoapods就不说了

创建一个CYLTabBarControllerConfig类

  1. #import <Foundation/Foundation.h>
  2. #import "CYLTabBarController.h"
  3. @interface CYLTabBarControllerConfig : NSObject
  4. @property (nonatomic, readonly, strong) CYLTabBarController *tabBarController;
  5. @end
  1. #import "CYLTabBarControllerConfig.h"
  2. @import Foundation;
  3. @import UIKit;
  4. @interface CYLBaseNavigationController : UINavigationController
  5. @end
  6. @implementation CYLBaseNavigationController
  7.  
  8. - (void)pushViewController:(UIViewController *)viewController animated:(BOOL)animated {
  9. if (self.viewControllers.count > ) {
  10. viewController.hidesBottomBarWhenPushed = YES;
  11. }
  12. [super pushViewController:viewController animated:animated];
  13. }
  14.  
  15. @end
  16.  
  17. //View Controllers
  18. #import "HandleViewController.h"
  19. #import "EnquiriesViewController.h"
  20. #import "ConsultViewController.h"
  21. #import "ManagerViewController.h"
  22. #import "ErectViewController.h"
  23.  
  24. @interface CYLTabBarControllerConfig ()
  25.  
  26. @property (nonatomic, readwrite, strong) CYLTabBarController *tabBarController;
  27.  
  28. @end
  29.  
  30. @implementation CYLTabBarControllerConfig
  31. /**
  32. * lazy load tabBarController
  33. *
  34. * @return CYLTabBarController
  35. */
  36. - (CYLTabBarController *)tabBarController {
  37. if (_tabBarController == nil) {
  38. HandleViewController *firstViewController = [[HandleViewController alloc] init];
  39. UIViewController *firstNavigationController = [[CYLBaseNavigationController alloc]
  40. initWithRootViewController:firstViewController];
  41.  
  42. EnquiriesViewController *secondViewController = [[EnquiriesViewController alloc] init];
  43. UIViewController *secondNavigationController = [[CYLBaseNavigationController alloc]
  44. initWithRootViewController:secondViewController];
  45. /**
  46. * 选项卡右上角的数字
  47. */
  48. // secondNavigationController.tabBarItem.badgeValue = @"10";
  49.  
  50. ConsultViewController *thirdViewController = [[ConsultViewController alloc] init];
  51. UIViewController *thirdNavigationController = [[CYLBaseNavigationController alloc]
  52. initWithRootViewController:thirdViewController];
  53.  
  54. ManagerViewController *fourthViewController = [[ManagerViewController alloc] init];
  55. UIViewController *fourthNavigationController = [[CYLBaseNavigationController alloc]
  56. initWithRootViewController:fourthViewController];
  57. ErectViewController *fifthViewControll = [[ErectViewController alloc]init];
  58. UIViewController *fifthNavigationController = [[CYLBaseNavigationController alloc]initWithRootViewController:fifthViewControll];
  59.  
  60. CYLTabBarController *tabBarController = [[CYLTabBarController alloc] init];
  61.  
  62. /**
  63. * 以下两行代码目的在于手动设置让TabBarItem只显示图标,不显示文字,并让图标垂直居中。
  64. * 等效于在`-setUpTabBarItemsAttributesForController`方法中不传`CYLTabBarItemTitle`字段。
  65. * 更推荐后一种做法。
  66. */
  67. //tabBarController.imageInsets = UIEdgeInsetsMake(4.5, 0, -4.5, 0);
  68. //tabBarController.titlePositionAdjustment = UIOffsetMake(0, MAXFLOAT);
  69.  
  70. // 在`-setViewControllers:`之前设置TabBar的属性,设置TabBarItem的属性,包括 title、Image、selectedImage。
  71. [self setUpTabBarItemsAttributesForController:tabBarController];
  72.  
  73. [tabBarController setViewControllers:@[
  74. firstNavigationController,
  75. secondNavigationController,
  76. thirdNavigationController,
  77. fourthNavigationController,
  78. fifthNavigationController
  79. ]];
  80. // 更多TabBar自定义设置:比如:tabBarItem 的选中和不选中文字和背景图片属性、tabbar 背景图片属性
  81. [self customizeTabBarAppearance:tabBarController];
  82. _tabBarController = tabBarController;
  83. }
  84. return _tabBarController;
  85. }
  86.  
  87. /**
  88. * 在`-setViewControllers:`之前设置TabBar的属性,设置TabBarItem的属性,包括 title、Image、selectedImage。
  89. */
  90. - (void)setUpTabBarItemsAttributesForController:(CYLTabBarController *)tabBarController {
  91.  
  92. NSDictionary *dict1 = @{
  93. CYLTabBarItemTitle : @"订单处理",
  94. CYLTabBarItemImage : @"tab_dingdanchuli",
  95. CYLTabBarItemSelectedImage : @"tab_dingdanchuli_s",
  96. };
  97. NSDictionary *dict2 = @{
  98. CYLTabBarItemTitle : @"订单查询",
  99. CYLTabBarItemImage : @"tab_dingdanchaxun",
  100. CYLTabBarItemSelectedImage : @"tab_dingdanchaxun_s",
  101. };
  102. NSDictionary *dict3 = @{
  103. CYLTabBarItemTitle : @"咨询",
  104. CYLTabBarItemImage : @"tab_xiaoxi",
  105. CYLTabBarItemSelectedImage : @"tab_xiaoxi_s",
  106. };
  107. NSDictionary *dict4 = @{
  108. CYLTabBarItemTitle : @"门店管理",
  109. CYLTabBarItemImage : @"tab_mendianguanli",
  110. CYLTabBarItemSelectedImage : @"tab_mendianguanli_s"
  111. };
  112. NSDictionary *dict5 = @{
  113. CYLTabBarItemTitle : @"设置",
  114. CYLTabBarItemImage : @"tab_shezhi",
  115. CYLTabBarItemSelectedImage : @"tab_shezhi_s"
  116. };
  117. NSArray *tabBarItemsAttributes = @[
  118. dict1,
  119. dict2,
  120. dict3,
  121. dict4,
  122. dict5
  123. ];
  124. tabBarController.tabBarItemsAttributes = tabBarItemsAttributes;
  125. }
  126.  
  127. /**
  128. * 更多TabBar自定义设置:比如:tabBarItem 的选中和不选中文字和背景图片属性、tabbar 背景图片属性
  129. */
  130. - (void)customizeTabBarAppearance:(CYLTabBarController *)tabBarController {
  131. // Customize UITabBar height
  132. // 自定义 TabBar 高度
  133. // tabBarController.tabBarHeight = 80.f;
  134.  
  135. // set the text color for unselected state
  136. // 普通状态下的文字属性
  137. NSMutableDictionary *normalAttrs = [NSMutableDictionary dictionary];
  138. normalAttrs[NSForegroundColorAttributeName] = [UIColor grayColor];
  139.  
  140. // set the text color for selected state
  141. // 选中状态下的文字属性
  142. NSMutableDictionary *selectedAttrs = [NSMutableDictionary dictionary];
  143. selectedAttrs[NSForegroundColorAttributeName] = RGBColor(, , );
  144.  
  145. // set the text Attributes
  146. // 设置文字属性
  147. UITabBarItem *tabBar = [UITabBarItem appearance];
  148. [tabBar setTitleTextAttributes:normalAttrs forState:UIControlStateNormal];
  149. [tabBar setTitleTextAttributes:selectedAttrs forState:UIControlStateSelected];
  150.  
  151. // Set the dark color to selected tab (the dimmed background)
  152. // TabBarItem选中后的背景颜色
  153. // [self customizeTabBarSelectionIndicatorImage];
  154.  
  155. // update TabBar when TabBarItem width did update
  156. // If your app need support UIDeviceOrientationLandscapeLeft or UIDeviceOrientationLandscapeRight,
  157. // remove the comment '//'
  158. // 如果你的App需要支持横竖屏,请使用该方法移除注释 '//'
  159. // [self updateTabBarCustomizationWhenTabBarItemWidthDidUpdate];
  160.  
  161. // set the bar shadow image
  162. // This shadow image attribute is ignored if the tab bar does not also have a custom background image.So at least set somthing.
  163. [[UITabBar appearance] setBackgroundImage:[[UIImage alloc] init]];
  164. [[UITabBar appearance] setBackgroundColor:[UIColor whiteColor]];
  165. [[UITabBar appearance] setShadowImage:[UIImage imageNamed:@"tapbar_top_line"]];
  166.  
  167. // set the bar background image
  168. // 设置背景图片
  169. // UITabBar *tabBarAppearance = [UITabBar appearance];
  170. // [tabBarAppearance setBackgroundImage:[UIImage imageNamed:@"tabbar_background"]];
  171.  
  172. // remove the bar system shadow image
  173. // 去除 TabBar 自带的顶部阴影
  174. // [[UITabBar appearance] setShadowImage:[[UIImage alloc] init]];
  175. }
  176.  
  177. - (void)updateTabBarCustomizationWhenTabBarItemWidthDidUpdate {
  178. void (^deviceOrientationDidChangeBlock)(NSNotification *) = ^(NSNotification *notification) {
  179. UIDeviceOrientation orientation = [[UIDevice currentDevice] orientation];
  180. if ((orientation == UIDeviceOrientationLandscapeLeft) || (orientation == UIDeviceOrientationLandscapeRight)) {
  181. NSLog(@"Landscape Left or Right !");
  182. } else if (orientation == UIDeviceOrientationPortrait){
  183. NSLog(@"Landscape portrait!");
  184. }
  185. [self customizeTabBarSelectionIndicatorImage];
  186. };
  187. [[NSNotificationCenter defaultCenter] addObserverForName:CYLTabBarItemWidthDidChangeNotification
  188. object:nil
  189. queue:[NSOperationQueue mainQueue]
  190. usingBlock:deviceOrientationDidChangeBlock];
  191. }
  192.  
  193. - (void)customizeTabBarSelectionIndicatorImage {
  194. ///Get initialized TabBar Height if exists, otherwise get Default TabBar Height.
  195. UITabBarController *tabBarController = [self cyl_tabBarController] ?: [[UITabBarController alloc] init];
  196. CGFloat tabBarHeight = tabBarController.tabBar.frame.size.height;
  197. CGSize selectionIndicatorImageSize = CGSizeMake(CYLTabBarItemWidth, tabBarHeight);
  198. //Get initialized TabBar if exists.
  199. UITabBar *tabBar = [self cyl_tabBarController].tabBar ?: [UITabBar appearance];
  200. [tabBar setSelectionIndicatorImage:
  201. [[self class] imageWithColor:[UIColor redColor]
  202. size:selectionIndicatorImageSize]];
  203. }
  204.  
  205. + (UIImage *)imageWithColor:(UIColor *)color size:(CGSize)size {
  206. if (!color || size.width <= || size.height <= ) return nil;
  207. CGRect rect = CGRectMake(0.0f, 0.0f, size.width + , size.height);
  208. UIGraphicsBeginImageContextWithOptions(rect.size, NO, );
  209. CGContextRef context = UIGraphicsGetCurrentContext();
  210. CGContextSetFillColorWithColor(context, color.CGColor);
  211. CGContextFillRect(context, rect);
  212. UIImage *image = UIGraphicsGetImageFromCurrentImageContext();
  213. UIGraphicsEndImageContext();
  214. return image;
  215. }
  216.  
  217. - (void)dealloc {
  218. [[NSNotificationCenter defaultCenter] removeObserver:self];
  219. }

另外还有自定义的加号按钮

要创建一个CYLPlusButton的类不要忘了加上CYLPlusButtonSubclassing协议

  1. #import <CYLTabBarController/CYLTabBarController.h>
  2.  
  3. @interface CYLPlusButtonSubclass : CYLPlusButton<CYLPlusButtonSubclassing,UIActionSheetDelegate>
  4.  
  5. @end
  1. #import "CYLPlusButtonSubclass.h"
  2. #import <CYLTabBarController.h>
  3.  
  4. @implementation CYLPlusButtonSubclass
  5.  
  6. +(void)load{
  7. [super registerSubclass];
  8. }
  9.  
  10. - (instancetype)initWithFrame:(CGRect)frame {
  11. if (self = [super initWithFrame:frame]) {
  12. self.titleLabel.textAlignment = NSTextAlignmentCenter;
  13. self.adjustsImageWhenHighlighted = NO;
  14.  
  15. }
  16. return self;
  17. }
  18. //上下结构的 button
  19. - (void)layoutSubviews {
  20. [super layoutSubviews];
  21.  
  22. // 控件大小,间距大小
  23. CGFloat const imageViewEdgeWidth = self.bounds.size.width * 0.7;
  24. CGFloat const imageViewEdgeHeight = imageViewEdgeWidth * 0.9;
  25. CGFloat const centerOfView = self.bounds.size.width * 0.5;
  26. CGFloat const labelLineHeight = self.titleLabel.font.lineHeight;
  27. CGFloat const verticalMarginT = self.bounds.size.height - labelLineHeight - imageViewEdgeWidth;
  28. CGFloat const verticalMargin = verticalMarginT / ;
  29.  
  30. // imageView 和 titleLabel 中心的 Y 值
  31. CGFloat const centerOfImageView = verticalMargin + imageViewEdgeWidth * 0.5;
  32. CGFloat const centerOfTitleLabel = imageViewEdgeWidth + verticalMargin * + labelLineHeight * 0.5 + ;
  33.  
  34. //imageView position 位置
  35. self.imageView.bounds = CGRectMake(, , imageViewEdgeWidth, imageViewEdgeHeight);
  36. self.imageView.center = CGPointMake(centerOfView, centerOfImageView);
  37.  
  38. //title position 位置
  39. self.titleLabel.bounds = CGRectMake(, , self.bounds.size.width, labelLineHeight);
  40. self.titleLabel.center = CGPointMake(centerOfView, centerOfTitleLabel);
  41. }
  42. /*
  43. *
  44. Create a custom UIButton with title and add it to the center of our tab bar
  45. *
  46. */
  47. + (id)plusButton {
  48. CYLPlusButtonSubclass *button = [[CYLPlusButtonSubclass alloc] init];
  49. UIImage *buttonImage = [UIImage imageNamed:@"post_normal"];
  50. [button setImage:buttonImage forState:UIControlStateNormal];
  51. [button setTitle:@"发布" forState:UIControlStateNormal];
  52. [button setTitleColor:[UIColor grayColor] forState:UIControlStateNormal];
  53.  
  54. [button setTitle:@"选中" forState:UIControlStateSelected];
  55. [button setTitleColor:[UIColor blueColor] forState:UIControlStateSelected];
  56.  
  57. button.titleLabel.font = [UIFont systemFontOfSize:9.5];
  58. [button sizeToFit]; // or set frame in this way `button.frame = CGRectMake(0.0, 0.0, 250, 100);`
  59. [button addTarget:button action:@selector(clickPublish) forControlEvents:UIControlEventTouchUpInside];
  60. return button;
  61. }
  62.  
  63. /*
  64. *
  65. Create a custom UIButton without title and add it to the center of our tab bar
  66. *
  67. */
  68. //+ (id)plusButton
  69. //{
  70. //
  71. // UIImage *buttonImage = [UIImage imageNamed:@"hood.png"];
  72. // UIImage *highlightImage = [UIImage imageNamed:@"hood-selected.png"];
  73. //
  74. // CYLPlusButtonSubclass* button = [CYLPlusButtonSubclass buttonWithType:UIButtonTypeCustom];
  75. //
  76. // button.autoresizingMask = UIViewAutoresizingFlexibleRightMargin | UIViewAutoresizingFlexibleLeftMargin | UIViewAutoresizingFlexibleBottomMargin | UIViewAutoresizingFlexibleTopMargin;
  77. // button.frame = CGRectMake(0.0, 0.0, buttonImage.size.width, buttonImage.size.height);
  78. // [button setBackgroundImage:buttonImage forState:UIControlStateNormal];
  79. // [button setBackgroundImage:highlightImage forState:UIControlStateHighlighted];
  80. // [button addTarget:button action:@selector(clickPublish) forControlEvents:UIControlEventTouchUpInside];
  81. //
  82. // return button;
  83. //}
  84. - (void)clickPublish {
  85. CYLTabBarController *tabBarController = [self cyl_tabBarController];
  86. UIViewController *viewController = tabBarController.selectedViewController;
  87. UIActionSheet *actionSheet = [[UIActionSheet alloc] initWithTitle:nil
  88. delegate:nil
  89. cancelButtonTitle:@"取消"
  90. destructiveButtonTitle:nil
  91. otherButtonTitles:@"拍照", @"从相册选取", @"淘宝一键转卖", nil];
  92. [actionSheet showInView:viewController.view];
  93. NSLog(@"发布");
  94. }
  95. #pragma mark - CYLPlusButtonSubclassing
  96. //+ (UIViewController *)plusChildViewController {
  97. // UIViewController *plusChildViewController = [[UIViewController alloc] init];
  98. // plusChildViewController.view.backgroundColor = [UIColor redColor];
  99. // plusChildViewController.navigationItem.title = @"PlusChildViewController";
  100. // UIViewController *plusChildNavigationController = [[UINavigationController alloc]
  101. // initWithRootViewController:plusChildViewController];
  102. // return plusChildNavigationController;
  103. //}
  104. //
  105. + (NSUInteger)indexOfPlusButtonInTabBar {
  106. return ;
  107. }
  108. + (CGFloat)multiplerInCenterY {
  109. return 0.3;
  110. }

如果正常按钮数目是奇数的话 要实现

  1. + (NSUInteger)indexOfPlusButtonInTabBar {
  2. return ;
  3. }

如何调整、自定义 PlusButton 与其它 TabBarItem 的宽度?

CYLTabBarController 规定:

  1. TabBarItem 宽度 ( TabBar 总宽度 PlusButton 宽度 ) / (TabBarItem 个数)

所以想自定义宽度,只需要修改 PlusButton 的宽度即可。

比如你就可以在 Demo中的 CYLPlusButtonSubclass.m 类里:

  1. [button sizeToFit]

改为

  1. button.frame = CGRectMake(0.0, 0.0, , );

---------------

如果要把加号按钮做成跟其他按钮的点击效果一样的话需要同时实现这两个方法

  1. + (UIViewController *)plusChildViewController {
  2. UIViewController *plusChildViewController = [[UIViewController alloc] init];
  3. plusChildViewController.view.backgroundColor = [UIColor redColor];
  4. plusChildViewController.navigationItem.title = @"PlusChildViewController";
  5. UIViewController *plusChildNavigationController = [[UINavigationController alloc]
  6. initWithRootViewController:plusChildViewController];
  7. return plusChildNavigationController;
  8. }
  9.  
  10. + (NSUInteger)indexOfPlusButtonInTabBar {
  11. return ;
  12. }

之前不知道怎么回事,出了些莫名其妙的问题。。记录下

前人种树后人乘凉啊!感谢原作作者

iOS 自定义选项卡-CYLTabBarController的更多相关文章

  1. 【iOS自定义键盘及键盘切换】详解

    [iOS自定义键盘]详解 实现效果展示: 一.实现的协议方法代码 #import <UIKit/UIKit.h> //创建自定义键盘协议 @protocol XFG_KeyBoardDel ...

  2. iOS自定义的UISwitch按钮

    UISwitch开关控件 开关代替了点选框.开关是到目前为止用起来最简单的控件,不过仍然可以作一定程度的定制化. 一.创建 UISwitch* mySwitch = [[ UISwitchalloc] ...

  3. 如何实现 iOS 自定义状态栏

    给大家介绍如何实现 iOS 自定义状态栏 Sample Code: 01 UIWindow * statusWindow = [[UIWindow alloc] initWithFrame:[UIAp ...

  4. excel快速访问工具栏和自定义选项卡

    自定义选项卡: excel命令选项--自定义功能区--

  5. iOS自定义组与组之间的距离以及视图

    iOS自定义组与组之间的距离以及视图 //头视图高度 - (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(N ...

  6. iOS 自定义转场动画

    代码地址如下:http://www.demodashi.com/demo/12955.html 一.总效果 本文记录分享下自定义转场动画的实现方法,具体到动画效果:新浪微博图集浏览转场效果.手势过渡动 ...

  7. iOS 自定义转场动画浅谈

    代码地址如下:http://www.demodashi.com/demo/11612.html 路漫漫其修远兮,吾将上下而求索 前记 想研究自定义转场动画很久了,时间就像海绵,挤一挤还是有的,花了差不 ...

  8. iOS自定义转场动画实战讲解

    iOS自定义转场动画实战讲解   转场动画这事,说简单也简单,可以通过presentViewController:animated:completion:和dismissViewControllerA ...

  9. SWT自定义选项卡CTabFolder

    SWT自定义选项卡CTabFolder 学习了:http://blog.csdn.net/dreajay/article/details/17391731 package com.swt; impor ...

随机推荐

  1. Bootstrap<基础四> 代码

    Bootstrap 允许您以两种方式显示代码: 第一种是 <code> 标签.如果您想要内联显示代码,那么您应该使用 <code> 标签. 第二种是 <pre> 标 ...

  2. lucene中Field.Index,Field.Store详解

    lucene在doc.add(new Field("content",curArt.getContent(),Field.Store.NO,Field.Index.TOKENIZE ...

  3. Web——在淘宝搜索到看到商品

    [摘自]http://blog.renren.com/blog/254459622/799372165 浏览器首先查询DNS服务器,将www.taobao.com转换成ip地址.负载均衡的第一步,将你 ...

  4. javascript base64 字符转换

    function Base64() { // private property _keyStr = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqr ...

  5. 手把手教你在ubuntu上安装apache和mysql和php

    1:首先安装apache:打开终端(ctrl+Alt+t), 输入命令:sudo apt-get install apache2即可安装, 安装完后,打开浏览器,在地址栏输入:localhost或者h ...

  6. MFC-01-Chapter01:Hello,MFC---1.3 第一个MFC程序(03)

    1.3.2 MFC如何使用应用程序对象 MFC程序没有main函数,没有WinMain函数,到底是什么启动了程序的运行? 一个MFC提供的源代码中(Winmain.cpp)包含了一个AfxWinMai ...

  7. <![CDATA[]]>作用

    <![CDATA[]]>的作用是让XML解析器将标签中的字符串当作普通文本对待,以防止某些字符串对XML格式造成破坏.

  8. 一次线上http接口调用不通相关的解决过程

    2016-05-25 08:58:34 昨天线上小白系统因为调用外部http接口,超时不释放,导致页面反应很慢,时间一长,报502错误. 上网查了下,502错误是因为服务对于客户的请求没有得到及时的反 ...

  9. Informatica - Powercenter 英文版资料(转载)

    Informatica - Powercenter 英文版资料 http://gerardnico.com/wiki/powercenter/powercenter

  10. jQuery深层次复制对象

    <script type="text/javascript" src="http://libs.baidu.com/jquery/1.9.1/jquery.min. ...