1.導入CYLTabBarController
使用cocoapods導入即可 pod 'CYLTabBarController', '~> 1.14.1',終端 pod install 即可
2.配置
新建一個基于 NSObject 類?MainTabBarControllerConfig,用于配置CYLTabBarController及管理窗骑;頭文件引入
#import <CYLTabBarController.h>
.h
@interface MainTabBarControllerConfig : NSObject
///CYLTabBarController
@property (nonatomic,strong) CYLTabBarController *mainTabBarController;@end
.m
- (CYLTabBarController *)mainTabBarController{
? ? if (!_mainTabBarController) {
? ? ? ? UIEdgeInsets imageInsets = UIEdgeInsetsZero;
? ? ? ? UIOffset titlePositionAdjustment = UIOffsetZero;
? ? ? ? _mainTabBarController = [CYLTabBarController tabBarControllerWithViewControllers:[self arrayViewControllerItem] tabBarItemsAttributes:[self arrayAttributesItem] imageInsets:imageInsets titlePositionAdjustment:titlePositionAdjustment];
? ? ? ? [self customizeTabBarAppearance:_mainTabBarController];
? ? }
? ? return _mainTabBarController;
}
- (NSArray *)arrayViewControllerItem{
? ? UINavigationController *firstNav = [[UINavigationController alloc] initWithRootViewController:[FirstViewController new]];
? ? UINavigationController *secondNav = [[UINavigationController alloc] initWithRootViewController:[SecondViewController new]];
? ? UINavigationController *thirdNav = [[UINavigationController alloc] initWithRootViewController:[ThirdViewController new]];
? ? UINavigationController *lastNav = [[UINavigationController alloc] initWithRootViewController:[LastViewController new]];
? ? ///返回需要加載的模塊
? ? return @[firstNav,secondNav,thirdNav,lastNav];
}
- (NSArray *)arrayAttributesItem{
? ? NSDictionary *bookcaseItemsAttributes =@{CYLTabBarItemTitle : @"首頁",
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? CYLTabBarItemImage : @"aa_unselect",
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? /* NSString and UIImage are supported*/
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? CYLTabBarItemSelectedImage : @"aa_select",};
? ? NSDictionary *discoverItemsAttributes = @{CYLTabBarItemTitle : @"產(chǎn)品",
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? CYLTabBarItemImage : @"bb_unselect",
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? CYLTabBarItemSelectedImage : @"bb_select",};
? ? NSDictionary *askklItemsAttributes = @{CYLTabBarItemTitle : @"工具",
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? CYLTabBarItemImage : @"cc_unselect",
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? CYLTabBarItemSelectedImage : @"cc_select",};
? ? NSDictionary *userItemsAttributes = @{CYLTabBarItemTitle : @"我的",
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? CYLTabBarItemImage : @"dd_unselect",
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? CYLTabBarItemSelectedImage : @"dd_select"};
? ? NSArray *tabBarItemsAttributes = @[bookcaseItemsAttributes,
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? discoverItemsAttributes,
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? askklItemsAttributes,
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? userItemsAttributes];
? ? return tabBarItemsAttributes;
}
/**
*? 更多TabBar自定義設置:比如:tabBarItem 的選中和不選中文字和背景圖片屬性、tabbar 背景圖片屬性等等
*/
- (void)customizeTabBarAppearance:(CYLTabBarController *)tabBarController {
? ? // Customize UITabBar height
? ? // 自定義 TabBar 高度
? ? //? ? tabBarController.tabBarHeight = CYLTabBarControllerHeight;
? ? // set the text color for unselected state
? ? // 普通狀態(tài)下的文字屬性
? ? NSMutableDictionary *normalAttrs = [NSMutableDictionary dictionary];
? ? normalAttrs[NSForegroundColorAttributeName] = [UIColor grayColor];
? ? // set the text color for selected state
? ? // 選中狀態(tài)下的文字屬性
? ? NSMutableDictionary *selectedAttrs = [NSMutableDictionary dictionary];
? ? selectedAttrs[NSForegroundColorAttributeName] = [UIColor greenColor];
? ? // set the text Attributes
? ? // 設置文字屬性
? ? UITabBarItem *tabBar = [UITabBarItem appearance];
? ? [tabBar setTitleTextAttributes:normalAttrs forState:UIControlStateNormal];
? ? [tabBar setTitleTextAttributes:selectedAttrs forState:UIControlStateSelected];
? ? // Set the dark color to selected tab (the dimmed background)
? ? // TabBarItem選中后的背景顏色
? ? // [self customizeTabBarSelectionIndicatorImage];
? ? // update TabBar when TabBarItem width did update
? ? // If your app need support UIDeviceOrientationLandscapeLeft or UIDeviceOrientationLandscapeRight,
? ? // remove the comment '//'
? ? // 如果你的App需要支持橫豎屏絮缅,請使用該方法移除注釋 '//'
? ? // [self updateTabBarCustomizationWhenTabBarItemWidthDidUpdate];
? ? // set the bar shadow image
? ? // This shadow image attribute is ignored if the tab bar does not also have a custom background image.So at least set somthing.
? ? [[UITabBar appearance] setBackgroundImage:[[UIImage alloc] init]];
? ? [[UITabBar appearance] setBackgroundColor:[UIColor whiteColor]];
? ? [[UITabBar appearance] setShadowImage:[UIImage imageNamed:@"tapbar_top_line"]];
? ? // set the bar background image
? ? // 設置背景圖片
? ? //? ? UITabBar *tabBarAppearance = [UITabBar appearance];
? ? //? ? [tabBarAppearance setBackgroundImage:[UIImage imageNamed:@"tab_bar"]];
? ? // remove the bar system shadow image
? ? // 去除 TabBar 自帶的頂部陰影
? ? // [[UITabBar appearance] setShadowImage:[[UIImage alloc] init]];
}
3.使用
在AppDelegate.m ?didFinishLaunchingWithOptions:(NSDictionary *)launchOptions 方法中
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
? ? // Override point for customization after application launch.
? ? // 創(chuàng)建Window
? ? self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
? ? // 設置Window的背景顏色
? ? self.window.backgroundColor = [UIColor whiteColor];
? ? // 設置根控制器
? ? MainTabBarControllerConfig *tabbarConfig = [[MainTabBarControllerConfig alloc]init];
? ? CYLTabBarController *mainTabbarController = tabbarConfig.mainTabBarController;
? ? [self.window setRootViewController:mainTabbarController];
? ? // 設置并顯示主窗口
? ? [self.window makeKeyAndVisible];
? ? return YES;
}
4.自定義按鈕
加入中間按鈕之前,確保上面的功能已經(jīng)實現(xiàn)呼股;新建一個基于?CYLPlusButton 的類?TabbarPlusButton耕魄,實現(xiàn)代理?CYLPlusButtonSubclassing
.h
@interface TabbarPlusButton : CYLPlusButton<CYLPlusButtonSubclassing>@end
.m
@implementation TabbarPlusButton
- (instancetype)initWithFrame:(CGRect)frame {
? ? if (self = [super initWithFrame:frame]) {
? ? ? ? self.titleLabel.textAlignment = NSTextAlignmentCenter;
? ? ? ? self.adjustsImageWhenHighlighted = NO;
? ? }
? ? return self;
}
//上下結(jié)構(gòu)的 button
- (void)layoutSubviews {
? ? [super layoutSubviews];
? ? // 控件大小,間距大小
? ? // 注意:一定要根據(jù)項目中的圖片去調(diào)整下面的0.7和0.9,Demo之所以這么設置彭谁,因為demo中的 plusButton 的 icon 不是正方形吸奴。
? ? CGFloat const imageViewEdgeWidth? = self.bounds.size.width * 0.7;
? ? CGFloat const imageViewEdgeHeight? = imageViewEdgeWidth * 0.9;
? ? CGFloat const centerOfView? ? = self.bounds.size.width * 0.5;
? ? CGFloat const labelLineHeight = self.titleLabel.font.lineHeight;
? ? CGFloat const verticalMargin? = (self.bounds.size.height - labelLineHeight - imageViewEdgeHeight) * 0.5;
? ? // imageView 和 titleLabel 中心的 Y 值
? ? CGFloat const centerOfImageView? = verticalMargin + imageViewEdgeHeight * 0.5;
? ? CGFloat const centerOfTitleLabel = imageViewEdgeHeight? + verticalMargin * 2 + labelLineHeight * 0.5 + 5;
? ? //imageView position 位置
? ? self.imageView.bounds = CGRectMake(0, 0, imageViewEdgeWidth, imageViewEdgeHeight);
? ? self.imageView.center = CGPointMake(centerOfView, centerOfImageView);
? ? //title position 位置
? ? self.titleLabel.bounds = CGRectMake(0, 0, self.bounds.size.width, labelLineHeight);
? ? self.titleLabel.center = CGPointMake(centerOfView, centerOfTitleLabel);
}
#pragma mark -
#pragma mark - CYLPlusButtonSubclassing Methods
/*
*
Create a custom UIButton with title and add it to the center of our tab bar
*
*/
+ (id)plusButton {
? ? TabbarPlusButton *button = [[TabbarPlusButton alloc] init];
? ? ///中間按鈕圖片
? ? UIImage *buttonImage = [UIImage imageNamed:@"post_normal"];
? ? [button setImage:buttonImage forState:UIControlStateNormal];
? ? [button setTitle:@"發(fā)布" forState:UIControlStateNormal];
? ? [button setTitleColor:[UIColor grayColor] forState:UIControlStateNormal];
? ? [button setTitle:@"發(fā)布" forState:UIControlStateSelected];
? ? [button setTitleColor:[UIColor blueColor] forState:UIControlStateSelected];
? ? button.titleLabel.font = [UIFont systemFontOfSize:9.5];
? ? [button sizeToFit]; // or set frame in this way `button.frame = CGRectMake(0.0, 0.0, 250, 100);`
? ? //? ? button.frame = CGRectMake(0.0, 0.0, 250, 100);
? ? //? ? button.backgroundColor = [UIColor redColor];
? ? // if you use `+plusChildViewController` , do not addTarget to plusButton.
? ? [button addTarget:button action:@selector(clickPublish) forControlEvents:UIControlEventTouchUpInside];
? ? return button;
}
/*
*
Create a custom UIButton without title and add it to the center of our tab bar
*
*/
//+ (id)plusButton
//{
//
//? ? UIImage *buttonImage = [UIImage imageNamed:@"hood.png"];
//? ? UIImage *highlightImage = [UIImage imageNamed:@"hood-selected.png"];
//
//? ? CYLPlusButtonSubclass* button = [CYLPlusButtonSubclass buttonWithType:UIButtonTypeCustom];
//
//? ? button.autoresizingMask = UIViewAutoresizingFlexibleRightMargin | UIViewAutoresizingFlexibleLeftMargin | UIViewAutoresizingFlexibleBottomMargin | UIViewAutoresizingFlexibleTopMargin;
//? ? button.frame = CGRectMake(0.0, 0.0, buttonImage.size.width, buttonImage.size.height);
//? ? [button setBackgroundImage:buttonImage forState:UIControlStateNormal];
//? ? [button setBackgroundImage:highlightImage forState:UIControlStateHighlighted];
//? ? [button addTarget:button action:@selector(clickPublish) forControlEvents:UIControlEventTouchUpInside];
//
//? ? return button;
//}
#pragma mark -
#pragma mark - Event Response
- (void)clickPublish {
? ? CYLTabBarController *tabBarController = [self cyl_tabBarController];
? ? UIViewController *viewController = tabBarController.selectedViewController;
? ? UIActionSheet *actionSheet = [[UIActionSheet alloc] initWithTitle:nil
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? delegate:nil
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? cancelButtonTitle:@"取消"
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? destructiveButtonTitle:nil
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? otherButtonTitles:@"拍照", @"從相冊選取", @"淘寶一鍵轉(zhuǎn)賣", nil];
? ? [actionSheet showInView:viewController.view];
}
#pragma mark - UIActionSheetDelegate
- (void)actionSheet:(UIActionSheet *)actionSheet didDismissWithButtonIndex:(NSInteger)buttonIndex {
? ? NSLog(@"buttonIndex = %@", @(buttonIndex));
}
#pragma mark - CYLPlusButtonSubclassing
//+ (UIViewController *)plusChildViewController {
//? ? UIViewController *plusChildViewController = [[UIViewController alloc] init];
//? ? plusChildViewController.view.backgroundColor = [UIColor redColor];
//? ? plusChildViewController.navigationItem.title = @"PlusChildViewController";
//? ? UIViewController *plusChildNavigationController = [[UINavigationController alloc]
//? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? initWithRootViewController:plusChildViewController];
//? ? return plusChildNavigationController;
//}
//
//+ (NSUInteger)indexOfPlusButtonInTabBar {
//? ? return 4;
//}
//
//+ (BOOL)shouldSelectPlusChildViewController {
//? ? BOOL isSelected = CYLExternPlusButton.selected;
//? ? if (isSelected) {
//? ? ? ? NSLog(@"??類名與方法名:%@(在第%@行),描述:%@", @(__PRETTY_FUNCTION__), @(__LINE__), @"PlusButton is selected");
//? ? } else {
//? ? ? ? NSLog(@"??類名與方法名:%@(在第%@行)缠局,描述:%@", @(__PRETTY_FUNCTION__), @(__LINE__), @"PlusButton is not selected");
//? ? }
//? ? return YES;
//}
+ (CGFloat)multiplierOfTabBarHeight:(CGFloat)tabBarHeight {
? ? return? 0.3;
}
+ (CGFloat)constantOfPlusButtonCenterYOffsetForTabBarHeight:(CGFloat)tabBarHeight {
? ? return? -10;
}
@end
最后在AppDelegate.m ?didFinishLaunchingWithOptions:(NSDictionary *)launchOptions 方法中加上?[TabbarPlusButton registerPlusButton] 即可;
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
? ? // Override point for customization after application launch.
[TabbarPlusButton registerPlusButton]
? ? // 創(chuàng)建Window
? ? self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
? ? // 設置Window的背景顏色
? ? self.window.backgroundColor = [UIColor whiteColor];
? ? // 設置根控制器
? ? MainTabBarControllerConfig *tabbarConfig = [[MainTabBarControllerConfig alloc]init];
? ? CYLTabBarController *mainTabbarController = tabbarConfig.mainTabBarController;
? ? [self.window setRootViewController:mainTabbarController];
? ? // 設置并顯示主窗口
? ? [self.window makeKeyAndVisible];
? ? return YES;
}