CYLTabBarController的使用

CYLTabBarController 是一個自定義的TabBarController, 集成非常簡單

https://github.com/ChenYilong/CYLTabBarController

1.首先使用CocoaPods 進行集成: 

pod 'CYLTabBarController'
在終端上執行: 
pod install --verbose --no-repo-update

2. 創建TabBar對應的視圖控制器


3.創建CYLTabBarControllerConfig

[objc] view plain copy
 在CODE上查看代碼片派生到我的代碼片
  1. #import <Foundation/Foundation.h>  
  2.   
  3. #import "CYLTabBarController.h"  
  4. @interface CYLTabBarControllerConfig : NSObject  
  5.   
  6. @property (nonatomicretainCYLTabBarController * tabBarController;  
  7.   
  8. @end  

[objc] view plain copy
 在CODE上查看代碼片派生到我的代碼片
  1. #import "CYLTabBarControllerConfig.h"  
  2.   
  3. #import "FirstViewController.h"  
  4. #import "SecondViewController.h"  
  5. #import "ThirdViewController.h"  
  6. #import "FourthViewController.h"  
  7.   
  8.   
  9. @implementation CYLTabBarControllerConfig  
  10.   
  11. - (CYLTabBarController *)tabBarController {  
  12.     if (_tabBarController == nil) {  
  13.         FirstViewController * firstViewController = [[FirstViewController alloc] init];  
  14.         UIViewController * firstNavigationController = [[UINavigationController alloc] initWithRootViewController:firstViewController];  
  15.           
  16.         SecondViewController * secondViewController = [[SecondViewController alloc] init];  
  17.         UIViewController * secondNavigationController = [[UINavigationController alloc] initWithRootViewController:secondViewController];  
  18.           
  19.         ThirdViewController * thirdViewController = [[ThirdViewController alloc] init];  
  20.         UIViewController * thirdNavigationController = [[UINavigationController alloc] initWithRootViewController:thirdViewController];  
  21.           
  22.         FourthViewController * fourthViewController = [[FourthViewController alloc] init];  
  23.         UIViewController * fourthNavigationController = [[UINavigationController alloc] initWithRootViewController:fourthViewController];  
  24.           
  25.           
  26.         NSArray * tabBarItemsAttributes = [self tabBarItemsAttributes];  
  27.         NSArray * viewControllers = @[firstNavigationController, secondNavigationController, thirdNavigationController, fourthNavigationController];  
  28.           
  29.         CYLTabBarController * tabBarController = [[CYLTabBarController alloc] init];  
  30.           
  31.         tabBarController.tabBarItemsAttributes = tabBarItemsAttributes;  
  32.         tabBarController.viewControllers = viewControllers;  
  33.           
  34.         _tabBarController = tabBarController;  
  35.           
  36.     }  
  37.       
  38.     return _tabBarController;  
  39. }  
  40.   
  41.   
  42. - (NSArray *)tabBarItemsAttributes {  
  43.     NSDictionary * tabBarItem1Attribute = @{  
  44.                                             CYLTabBarItemTitle : @"首頁",  
  45.                                             CYLTabBarItemImage : @"home_normal",  
  46.                                             CYLTabBarItemSelectedImage : @"home_highlight"  
  47.                                    };  
  48.     NSDictionary * tabBarItem2Attribute = @{  
  49.                                             CYLTabBarItemTitle : @"同城",  
  50.                                             CYLTabBarItemImage : @"mycity_normal",  
  51.                                             CYLTabBarItemSelectedImage : @"mycity_highlight"  
  52.                                             };  
  53.     NSDictionary * tabBarItem3Attribute = @{  
  54.                                             CYLTabBarItemTitle : @"消息",  
  55.                                             CYLTabBarItemImage : @"message_normal",  
  56.                                             CYLTabBarItemSelectedImage : @"message_highlight"  
  57.                                             };  
  58.     NSDictionary * tabBarItem4Attribute = @{  
  59.                                             CYLTabBarItemTitle : @"我的",  
  60.                                             CYLTabBarItemImage : @"account_normal",  
  61.                                             CYLTabBarItemSelectedImage : @"account_highlight"  
  62.                                             };  
  63.     NSArray * tarBarItemsAttrbutes = @[tabBarItem1Attribute, tabBarItem2Attribute, tabBarItem3Attribute, tabBarItem4Attribute];  
  64.       
  65.     return tarBarItemsAttrbutes;  
  66. }  
  67.   
  68.   
  69. /** 
  70.  *  更多TabBar自定義設置:比如:tabBarItem 的選中和不選中文字和背景圖片屬性、tabbar 背景圖片屬性 
  71.  */  
  72. + (void)customizeTabBarAppearance {  
  73.       
  74.     //去除 TabBar 自帶的頂部陰影  
  75.     [[UITabBar appearance] setShadowImage:[[UIImage alloc] init]];  
  76.       
  77.     // set the text color for unselected state  
  78.     // 普通狀態下的文字屬性  
  79.     NSMutableDictionary *normalAttrs = [NSMutableDictionary dictionary];  
  80.     normalAttrs[NSForegroundColorAttributeName] = [UIColor blackColor];  
  81.       
  82.     // set the text color for selected state  
  83.     // 選中狀態下的文字屬性  
  84.     NSMutableDictionary *selectedAttrs = [NSMutableDictionary dictionary];  
  85.     selectedAttrs[NSForegroundColorAttributeName] = [UIColor blackColor];  
  86.       
  87.     // set the text Attributes  
  88.     // 設置文字屬性  
  89.     UITabBarItem *tabBar = [UITabBarItem appearance];  
  90.     [tabBar setTitleTextAttributes:normalAttrs forState:UIControlStateNormal];  
  91.     [tabBar setTitleTextAttributes:selectedAttrs forState:UIControlStateSelected];  
  92.       
  93.     // Set the dark color to selected tab (the dimmed background)  
  94.     // TabBarItem選中後的背景顏色  
  95.     [[UITabBar appearance] setSelectionIndicatorImage:[self imageFromColor:[UIColor colorWithRed:26 / 255.0 green:163 / 255.0 blue:133 / 255.0 alpha:1] forSize:CGSizeMake([UIScreen mainScreen].bounds.size.width / 5.0f49) withCornerRadius:0]];  
  96.       
  97.     // set the bar background color  
  98.     // 設置背景圖片  
  99.     // UITabBar *tabBarAppearance = [UITabBar appearance];  
  100.     // [tabBarAppearance setBackgroundImage:[UIImage imageNamed:@"tabbar_background_ios7"]];  
  101. }  
  102.   
  103. + (UIImage *)imageFromColor:(UIColor *)color forSize:(CGSize)size withCornerRadius:(CGFloat)radius {  
  104.     CGRect rect = CGRectMake(00, size.width, size.height);  
  105.     UIGraphicsBeginImageContext(rect.size);  
  106.       
  107.     CGContextRef context = UIGraphicsGetCurrentContext();  
  108.     CGContextSetFillColorWithColor(context, [color CGColor]);  
  109.     CGContextFillRect(context, rect);  
  110.       
  111.     UIImage *image = UIGraphicsGetImageFromCurrentImageContext();  
  112.     UIGraphicsEndImageContext();  
  113.       
  114.     // Begin a new image that will be the new image with the rounded corners  
  115.     // (here with the size of an UIImageView)  
  116.     UIGraphicsBeginImageContext(size);  
  117.       
  118.     // Add a clip before drawing anything, in the shape of an rounded rect  
  119.     [[UIBezierPath bezierPathWithRoundedRect:rect cornerRadius:radius] addClip];  
  120.     // Draw your image  
  121.     [image drawInRect:rect];  
  122.       
  123.     // Get the image, here setting the UIImageView image  
  124.     image = UIGraphicsGetImageFromCurrentImageContext();  
  125.       
  126.     // Lets forget about that we were drawing  
  127.     UIGraphicsEndImageContext();  
  128.     return image;  
  129. }  


4. AppDelegate 設置根視圖控制器

[objc] view plain copy
 在CODE上查看代碼片派生到我的代碼片
  1. - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {  
  2.     // TabBar  
  3.     CYLTabBarControllerConfig * TabBarControllerConfig = [[CYLTabBarControllerConfig alloc] init];  
  4.     self.window.rootViewController = TabBarControllerConfig.tabBarController;  
  5.     [self customizeInterface];  
  6.       
  7.     return YES;  
  8. }  
  9. - (void)customizeInterface {  
  10.     [self setUpNavigationBarAppearance];  
  11. }  
  12.   
  13. /** 
  14.  *  設置navigationBar樣式 
  15.  */  
  16. - (void)setUpNavigationBarAppearance {  
  17.     UINavigationBar *navigationBarAppearance = [UINavigationBar appearance];  
  18.       
  19.     UIImage *backgroundImage = nil;  
  20.     NSDictionary *textAttributes = nil;  
  21.     if (NSFoundationVersionNumber > NSFoundationVersionNumber_iOS_6_1) {  
  22.         backgroundImage = [UIImage imageNamed:@"navigationbar_background_tall"];  
  23.           
  24.         textAttributes = @{  
  25.                            NSFontAttributeName: [UIFont boldSystemFontOfSize:18],  
  26.                            NSForegroundColorAttributeName: [UIColor blackColor],  
  27.                            };  
  28.     } else {  
  29. #if __IPHONE_OS_VERSION_MIN_REQUIRED < __IPHONE_7_0  
  30.         backgroundImage = [UIImage imageNamed:@"navigationbar_background"];  
  31.           
  32.         textAttributes = @{  
  33.                            UITextAttributeFont: [UIFont boldSystemFontOfSize:18],  
  34.                            UITextAttributeTextColor: [UIColor blackColor],  
  35.                            UITextAttributeTextShadowColor: [UIColor clearColor],  
  36.                            UITextAttributeTextShadowOffset: [NSValue valueWithUIOffset:UIOffsetZero],  
  37.                            };  
  38. #endif  
  39.     }  
  40.       
  41.     [navigationBarAppearance setBackgroundImage:backgroundImage  
  42.                                   forBarMetrics:UIBarMetricsDefault];  
  43.     [navigationBarAppearance setTitleTextAttributes:textAttributes];  
  44. }  

運行即可實現效果,如果想實現凸起的加號效果需要 CYLPlusButtonSubclass

[objc] view plain copy
 在CODE上查看代碼片派生到我的代碼片
  1. #import "CYLPlusButton.h"  
  2.   
  3. @interface CYLPlusButtonSubclass : CYLPlusButton <CYLPlusButtonSubclassing>  
  4.   
  5. @end  

[objc] view plain copy
 在CODE上查看代碼片派生到我的代碼片
  1. #import "CYLPlusButtonSubclass.h"  
  2.   
  3. @interface CYLPlusButtonSubclass ()<UIActionSheetDelegate> {  
  4.     CGFloat _buttonImageHeight;  
  5. }  
  6.   
  7. @end  
  8.   
  9. @implementation CYLPlusButtonSubclass  
  10.   
  11. #pragma mark -  
  12. #pragma mark - Life Cycle  
  13.   
  14. + (void)load {  
  15.     [super registerSubclass];  
  16. }  
  17.   
  18. - (instancetype)initWithFrame:(CGRect)frame {  
  19.     if (self = [super initWithFrame:frame]) {  
  20.         self.titleLabel.textAlignment = NSTextAlignmentCenter;  
  21.         self.adjustsImageWhenHighlighted = NO;  
  22.     }  
  23.     return self;  
  24. }  
  25.   
  26. //上下結構的 button  
  27. - (void)layoutSubviews {  
  28.     [super layoutSubviews];  
  29.       
  30.     // 控件大小,間距大小  
  31.     CGFloat const imageViewEdge   = self.bounds.size.width * 0.6;  
  32.     CGFloat const centerOfView    = self.bounds.size.width * 0.5;  
  33.     CGFloat const labelLineHeight = self.titleLabel.font.lineHeight;  
  34.     CGFloat const verticalMarginT = self.bounds.size.height - labelLineHeight - imageViewEdge;  
  35.     CGFloat const verticalMargin  = verticalMarginT / 2;  
  36.       
  37.     // imageView 和 titleLabel 中心的 Y 值  
  38.     CGFloat const centerOfImageView  = verticalMargin + imageViewEdge * 0.5;  
  39.     CGFloat const centerOfTitleLabel = imageViewEdge  + verticalMargin * 2 + labelLineHeight * 0.5 + 5;  
  40.       
  41.     //imageView position 位置  
  42.     self.imageView.bounds = CGRectMake(00, imageViewEdge, imageViewEdge);  
  43.     self.imageView.center = CGPointMake(centerOfView, centerOfImageView);  
  44.       
  45.     //title position 位置  
  46.     self.titleLabel.bounds = CGRectMake(00self.bounds.size.width, labelLineHeight);  
  47.     self.titleLabel.center = CGPointMake(centerOfView, centerOfTitleLabel);  
  48. }  
  49.   
  50. #pragma mark -  
  51. #pragma mark - Public Methods  
  52.   
  53. /* 
  54.  * 
  55.  Create a custom UIButton with title and add it to the center of our tab bar 
  56.  * 
  57.  */  
  58. + (instancetype)plusButton {  
  59.       
  60.     CYLPlusButtonSubclass *button = [[CYLPlusButtonSubclass alloc] init];  
  61.       
  62.     [button setImage:[UIImage imageNamed:@"post_normal"] forState:UIControlStateNormal];  
  63.     [button setTitle:@"發佈" forState:UIControlStateNormal];  
  64.       
  65.     [button setTitleColor:[UIColor grayColor] forState:UIControlStateNormal];  
  66.     button.titleLabel.font = [UIFont systemFontOfSize:9.5];  
  67.     [button sizeToFit];  
  68.       
  69.     [button addTarget:button action:@selector(clickPublish) forControlEvents:UIControlEventTouchUpInside];  
  70.     return button;  
  71. }  
  72. /* 
  73.  * 
  74.  Create a custom UIButton without title and add it to the center of our tab bar 
  75.  * 
  76.  */  
  77. //+ (instancetype)plusButton  
  78. //{  
  79. //  
  80. //    UIImage *buttonImage = [UIImage imageNamed:@"hood.png"];  
  81. //    UIImage *highlightImage = [UIImage imageNamed:@"hood-selected.png"];  
  82. //  
  83. //    CYLPlusButtonSubclass* button = [CYLPlusButtonSubclass buttonWithType:UIButtonTypeCustom];  
  84. //  
  85. //    button.autoresizingMask = UIViewAutoresizingFlexibleRightMargin | UIViewAutoresizingFlexibleLeftMargin | UIViewAutoresizingFlexibleBottomMargin | UIViewAutoresizingFlexibleTopMargin;  
  86. //    button.frame = CGRectMake(0.0, 0.0, buttonImage.size.width, buttonImage.size.height);  
  87. //    [button setBackgroundImage:buttonImage forState:UIControlStateNormal];  
  88. //    [button setBackgroundImage:highlightImage forState:UIControlStateHighlighted];  
  89. //    [button addTarget:button action:@selector(clickPublish) forControlEvents:UIControlEventTouchUpInside];  
  90. //  
  91. //    return button;  
  92. //}  
  93.   
  94. #pragma mark -  
  95. #pragma mark - Event Response  
  96.   
  97. - (void)clickPublish {  
  98.     UITabBarController *tabBarController = (UITabBarController *)self.window.rootViewController;  
  99.     UIViewController *viewController = tabBarController.selectedViewController;  
  100.       
  101.     UIActionSheet *actionSheet = [[UIActionSheet alloc] initWithTitle:nil  
  102.                                                              delegate:self  
  103.                                                     cancelButtonTitle:@"取消"  
  104.                                                destructiveButtonTitle:nil  
  105.                                                     otherButtonTitles:@"拍照"@"從相冊選取"@"淘寶一鍵轉賣", nil nil];  
  106.     [actionSheet showInView:viewController.view];  
  107. }  
  108.   
  109. #pragma mark - UIActionSheetDelegate  
  110. - (void)actionSheet:(UIActionSheet *)actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex {  
  111.     NSLog(@"index: %ld", buttonIndex);  
  112. }  
  113.   
  114.   
  115. #pragma mark - CYLPlusButtonSubclassing  
  116. //+ (NSUInteger)indexOfPlusButtonInTabBar {  
  117. //    return 3;  
  118. //}  
  119.   
  120. + (CGFloat)multiplerInCenterY {  
  121.     return  0.3;  
  122. }  
  123.   
  124. @end  

Demo 下載地址: 

http://download.csdn.net/detail/vbirdbest/9431253

實現效果如圖:



發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章