** UITabbarController控制器可以將軟件按照模塊進(jìn)行很好的劃分,也是主流軟件架構(gòu)的window的根控制器**
工作中,我一般會聲明一個UITabbarController的子類作為window的根控制器,并實現(xiàn)其代理方法。
UITabBarController.h分析
屬性
子控制器數(shù)組
@property(nullable, nonatomic,copy) NSArray<__kindof UIViewController *> *viewControllers;
設(shè)置子控制器數(shù)組
- (void)setViewControllers:(NSArray *)viewControllers animated:(BOOL)animated;
當(dāng)前選擇控制器
@property(nullable, nonatomic, assign)UIViewController *selectedViewController;
當(dāng)前選擇控制器索引
@property(nonatomic) NSUInteger selectedIndex;
這個沒用過 也不曉得干嘛使
@property(nonatomic, readonly) UINavigationController *moreNavigationController;
這個沒用過 也不曉得干嘛使
@property(nullable, nonatomic, copy) NSArray *customizableViewControllers;
tabbar底部標(biāo)簽欄
@property(nonatomic,readonly) UITabBar *tabBar ;
代理
@property(nullable, nonatomic,weak) id<UITabBarControllerDelegate> delegate;
代理方法
/**
* 點擊Item時調(diào)用:控制哪些 ViewController 的標(biāo)簽欄能被點擊
*
* @param tabBarController 當(dāng)前tabbar控制器
* @param viewController 將要點擊的目標(biāo)控制器
*
* @return YES:允許點擊 NO:不允許點擊
*/
- (BOOL)tabBarController:(UITabBarController *)tabBarController shouldSelectViewController:(UIViewController *)viewController NS_AVAILABLE_IOS(3_0);
/**
* 點擊Item時調(diào)用
*
* @param tabBarController 當(dāng)前tabbar控制器
* @param viewController 將要點擊的目標(biāo)控制器
*/
- (void)tabBarController:(UITabBarController *)tabBarController didSelectViewController:(UIViewController *)viewController;
/**
* 監(jiān)控開始初始化子控制器
*
* @param tabBarController 當(dāng)前tabbar控制器
* @param viewControllers 子控制器
*/
- (void)tabBarController:(UITabBarController *)tabBarController willBeginCustomizingViewControllers:(NSArray<__kindof UIViewController *> *)viewControllers NS_AVAILABLE_IOS(3_0) __TVOS_PROHIBITED;
/**
* 監(jiān)控初始化子控制器完成
*
* @param tabBarController 當(dāng)前tabbar控制器
* @param viewControllers 子控制器
* @param changed 是否改變
*/
- (void)tabBarController:(UITabBarController *)tabBarController willEndCustomizingViewControllers:(NSArray<__kindof UIViewController *> *)viewControllers changed:(BOOL)changed NS_AVAILABLE_IOS(3_0) __TVOS_PROHIBITED;
/**
* 監(jiān)控初始化子控制器完成
*
* @param tabBarController 當(dāng)前tabbar控制器
* @param viewControllers 子控制器
* @param changed 是否改變
*/
- (void)tabBarController:(UITabBarController *)tabBarController didEndCustomizingViewControllers:(NSArray<__kindof UIViewController *> *)viewControllers changed:(BOOL)changed __TVOS_PROHIBITED;
/**
* 監(jiān)控橫豎屏切換
*
* @param tabBarController tabbar控制器
*
* @return 橫豎屏切換枚舉
*/
- (UIInterfaceOrientationMask)tabBarControllerSupportedInterfaceOrientations:(UITabBarController *)tabBarController NS_AVAILABLE_IOS(7_0) __TVOS_PROHIBITED;
/**
* 監(jiān)控橫豎屏切換
*
* @param tabBarController tabbar控制器
*
* @return 橫豎屏切換枚舉
*/
- (UIInterfaceOrientation)tabBarControllerPreferredInterfaceOrientationForPresentation:(UITabBarController *)tabBarController NS_AVAILABLE_IOS(7_0) __TVOS_PROHIBITED;
/**
* 定制轉(zhuǎn)場動畫
*
* @param tabBarController 當(dāng)前tabbar控制器
* @param animationController 動畫控制器
*
* @return 轉(zhuǎn)場動畫
*/
- (nullable id <UIViewControllerInteractiveTransitioning>)tabBarController:(UITabBarController *)tabBarController
interactionControllerForAnimationController: (id <UIViewControllerAnimatedTransitioning>)animationController NS_AVAILABLE_IOS(7_0);
/**
* 定制轉(zhuǎn)場動畫
*
* @param tabBarController 當(dāng)前tabbar控制器
* @param fromVC 起點控制器
* @param toVC 目標(biāo)控制器
*
* @return 轉(zhuǎn)場動畫
*/
- (nullable id <UIViewControllerAnimatedTransitioning>)tabBarController:(UITabBarController *)tabBarController
animationControllerForTransitionFromViewController:(UIViewController *)fromVC
toViewController:(UIViewController *)toVC NS_AVAILABLE_IOS(7_0);
UIViewController分類
給每一個視圖控制器拓展一個tabBarItem和tabBarController
@interface UIViewController (UITabBarControllerItem)
@property(null_resettable, nonatomic, strong) UITabBarItem *tabBarItem; // Automatically created lazily with the view controller's title if it's not set explicitly.
@property(nullable, nonatomic, readonly, strong) UITabBarController *tabBarController; // If the view controller has a tab bar controller as its ancestor, return it. Returns nil otherwise.
@end
UITabBarItem分析
** UITabBarItem繼承于UIBarItem ,我們先分析下UIBarItem**
/**
* 是否可點擊
*/
@property(nonatomic,getter=isEnabled) BOOL enabled; // default is YES
/**
* item的title
*/
@property(nullable, nonatomic,copy) NSString *title; // default is nil
/**
* item的Image
*/
@property(nullable, nonatomic,strong) UIImage *image; // default is nil
@property(nullable, nonatomic,strong) UIImage *landscapeImagePhone NS_AVAILABLE_IOS(5_0) __TVOS_PROHIBITED; // default is nil
/**
* item圖片的邊框
*/
@property(nonatomic) UIEdgeInsets imageInsets; // default is UIEdgeInsetsZero
@property(nonatomic) UIEdgeInsets landscapeImagePhoneInsets NS_AVAILABLE_IOS(5_0) __TVOS_PROHIBITED; // default is UIEdgeInsetsZero. These insets apply only when the landscapeImagePhone property is set.
@property(nonatomic) NSInteger tag; // default is 0
/* You may specify the font, text color, and shadow properties for the title in the text attributes dictionary, using the keys found in NSAttributedString.h.
*/
/**
* 設(shè)置title屬性
*
* @param attributes 文字屬性
* @param state item狀態(tài)
*/
- (void)setTitleTextAttributes:(nullable NSDictionary<NSString *,id> *)attributes forState:(UIControlState)state NS_AVAILABLE_IOS(5_0) UI_APPEARANCE_SELECTOR;
/**
* 獲取item屬性字典
*
* @param state item狀態(tài)
*
* @return item屬性字典
*/
- (nullable NSDictionary<NSString *,id> *)titleTextAttributesForState:(UIControlState)state NS_AVAILABLE_IOS(5_0) UI_APPEARANCE_SELECTOR;
Demo(Objective-C)
AppDelegate設(shè)置window的根控制器
#import "AppDelegate.h"
#import "MYFTabbarController.h"
@interface AppDelegate ()
@property (nonatomic, strong) MYFTabbarController *tabbar;
@end
@implementation AppDelegate
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
// Override point for customization after application launch.
self.tabbar = [[MYFTabbarController alloc]init];
self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
[self.window setRootViewController:self.tabbar];
[self.window makeKeyAndVisible];
return YES;
}
自定義TabbarController
#import "MYFTabbarController.h"
@interface MYFTabbarController ()
@end
@implementation MYFTabbarController
- (void)viewDidLoad {
[super viewDidLoad];
//聲明子控制器
[self setChildVC];
}
- (void)setChildVC{
UIViewController *vc1 = [[UIViewController alloc]init];
vc1.view.backgroundColor = [UIColor yellowColor];
vc1.title = @"vc1";
vc1.tabBarItem.title = @"vc1";
vc1.tabBarItem.image = [self imageFromColor:[UIColor yellowColor] andFrame:CGRectMake(0, 0, 33, 33)];
UINavigationController *nav1 = [[UINavigationController alloc]initWithRootViewController:vc1];
UIViewController *vc2 = [[UIViewController alloc]init];
vc2.title = @"vc2";
vc2.tabBarItem.title = @"vc2";
vc2.view.backgroundColor = [UIColor blueColor];
vc2.tabBarItem.image = [self imageFromColor:[UIColor blueColor] andFrame:CGRectMake(0, 0, 33, 33)];
UINavigationController *nav2 = [[UINavigationController alloc]initWithRootViewController:vc2];
UIViewController *vc3 = [[UIViewController alloc]init];
vc3.title = @"vc3";
vc3.tabBarItem.title = @"vc3";
vc3.view.backgroundColor = [UIColor redColor];
vc3.tabBarItem.image = [self imageFromColor:[UIColor redColor] andFrame:CGRectMake(0, 0, 33, 33)];
UINavigationController *nav3 = [[UINavigationController alloc]initWithRootViewController:vc3];
self.viewControllers = @[nav1,nav2,nav3];
}
- (UIImage *)imageFromColor:(UIColor *)color andFrame:(CGRect)frame{
CGRect rect = frame;
UIGraphicsBeginImageContext(rect.size);
CGContextRef context = UIGraphicsGetCurrentContext();
CGContextSetFillColorWithColor(context, [color CGColor]);
CGContextFillRect(context, rect);
UIImage *img = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
return img;
}
@end
效果
這時候你會發(fā)現(xiàn)item的select狀態(tài)下是經(jīng)過渲染的,開發(fā)中我們也大部分用自定義item的圖片,而不用渲染后的結(jié)果
UITabbarItem禁止渲染
通過UIImage的方法來禁止圖片渲染
// Create a version of this image with the specified rendering mode. By default, images have a rendering mode of UIImageRenderingModeAutomatic.
/**
* 設(shè)置圖片Model
*
* @param renderingMode
typedef NS_ENUM(NSInteger, UIImageRenderingMode) {
UIImageRenderingModeAutomatic, // Use the default rendering mode for the context where the image is used 默認(rèn)模式,自動渲染
UIImageRenderingModeAlwaysOriginal, // Always draw the original image, without treating it as a template 原始模式,原始圖片
UIImageRenderingModeAlwaysTemplate, // Always draw the image as a template image, ignoring its color information 簡易模式,忽視圖片顏色
} NS_ENUM_AVAILABLE_IOS(7_0);
*
* @return <#return value description#>
*/
- (UIImage *)imageWithRenderingMode:(UIImageRenderingMode)renderingMode NS_AVAILABLE_IOS(7_0);
vc1.tabBarItem.selectedImage = [[self imageFromColor:[UIColor yellowColor] andFrame:CGRectMake(0, 0, 33, 33)] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
vc1.tabBarItem.image = [[self imageFromColor:[UIColor lightGrayColor] andFrame:CGRectMake(0, 0, 33, 33)] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];