UItabBarController

从几方面认识UItabBarController


1.UITabBarController & UINavigationController的区别:

导航视图控制器 -->管理有层次关系的视图控制器(上下关系)
标签视图控制器 -->管理没有层次关系的视图控制器(平等的关系,互不干扰)

管理方式不同

导航视图控制器 -->以栈的形式管理多个试图控制器,push入栈,pop出栈,当返回上一界面时,空间回收
标签视图控制器 -->以不可变数组管理,而且创建时必须要全部指定所管理的多个视图控制器,而且多个视图控制器同时存在,空间不回收

2.APP视图层次添加过程:

必须遵循该添加层次描述, 否则可能出现覆盖和不显示等问题!!!
UIWindow—>UITabBarcontroller—>UINavigationController— >UIViewController

3.继承分析UITabBarController

UItabBarController_第1张图片
1BE7D779-FC3C-4633-8409-95F804A51E30.png

由图可知,UITabBarController是继承于UIViewContrller的。

//
//  UITabBarController.h
//  UIKit
//
//  Copyright (c) 2007-2015 Apple Inc. All rights reserved.
//

#import 
#import 
#import 
#import 
#import 

NS_ASSUME_NONNULL_BEGIN

@class UIView, UIImage, UINavigationController, UITabBarItem;
@protocol UITabBarControllerDelegate; 

NS_CLASS_AVAILABLE_IOS(2_0) @interface UITabBarController : UIViewController  

*****************属性********************
 // 以数组的方式设置子控制器
@property(nullable, nonatomic,copy) NSArray<__kindof UIViewController *> *viewControllers; 
//以数组的方式设置子控制器
- (void)setViewControllers:(NSArray<__kindof UIViewController *> * __nullable)viewControllers animated:(BOOL)animated;

// 设置选中的子控制器,设置默认显示的子控制器
@property(nullable, nonatomic, assign) __kindof UIViewController *selectedViewController; 

// 通过设置选中的子控制的索引,设置默认显示的子控制器的索引
@property(nonatomic) NSUInteger selectedIndex;

// moreNavigationController是默认创建的,只有子控制器的数量超过5个的时候才显示”More”按钮
@property(nonatomic, readonly) UINavigationController *moreNavigationController __TVOS_PROHIBITED; 

// 在”More”的控制器视图中,有一个“Edit”按钮,设置这个按钮里面显示的自定义控制器,默认是所有控制器
// 注意:当UITabBarController的viewControllers属性发生变化的时候,customizableViewControllers就会自动设置成跟viewControllers一致
@property(nullable, nonatomic, copy) NSArray<__kindof UIViewController *> *customizableViewControllers __TVOS_PROHIBITED;

//// 只读,标签栏,默认生成
@property(nonatomic,readonly) UITabBar *tabBar NS_AVAILABLE_IOS(3_0); 


*****************代理********************
@property(nullable, nonatomic,weak) id delegate;

@end

@protocol UITabBarControllerDelegate 
@optional


//   视图将要切换时调用,   viewController为将要显示的控制器
- (BOOL)tabBarController:(UITabBarController *)tabBarController shouldSelectViewController:(UIViewController *)viewController NS_AVAILABLE_IOS(3_0);

//   视图已经切换后调用,   viewController   是已经显示的控制器
- (void)tabBarController:(UITabBarController *)tabBarController didSelectViewController:(UIViewController *)viewController;

//当即将点击”More”的控制器视图里的“Edit”按钮时调用
- (void)tabBarController:(UITabBarController *)tabBarController willBeginCustomizingViewControllers:(NSArray<__kindof UIViewController *> *)viewControllers NS_AVAILABLE_IOS(3_0) __TVOS_PROHIBITED;

// 点击”More”的控制器视图里的“Edit”按钮,在即将退出编辑时调用
- (void)tabBarController:(UITabBarController *)tabBarController willEndCustomizingViewControllers:(NSArray<__kindof UIViewController *> *)viewControllers changed:(BOOL)changed NS_AVAILABLE_IOS(3_0) __TVOS_PROHIBITED;

// 点击”More”的控制器视图里的“Edit”按钮,在退出编辑后调用
- (void)tabBarController:(UITabBarController *)tabBarController didEndCustomizingViewControllers:(NSArray<__kindof UIViewController *> *)viewControllers changed:(BOOL)changed __TVOS_PROHIBITED;



// taBarcontroller支持的接口方向 (学习)
- (UIInterfaceOrientationMask)tabBarControllerSupportedInterfaceOrientations:(UITabBarController *)tabBarController NS_AVAILABLE_IOS(7_0) __TVOS_PROHIBITED;
- (UIInterfaceOrientation)tabBarControllerPreferredInterfaceOrientationForPresentation:(UITabBarController *)tabBarController NS_AVAILABLE_IOS(7_0) __TVOS_PROHIBITED;
- (nullable id )tabBarController:(UITabBarController *)tabBarController
                      interactionControllerForAnimationController: (id )animationController NS_AVAILABLE_IOS(7_0);
- (nullable id )tabBarController:(UITabBarController *)tabBarController
            animationControllerForTransitionFromViewController:(UIViewController *)fromVC
                                              toViewController:(UIViewController *)toVC  NS_AVAILABLE_IOS(7_0);

@end

// *****************类别********************
@interface UIViewController (UITabBarControllerItem)

@property(null_resettable, nonatomic, strong) UITabBarItem *tabBarItem; 

@property(nullable, nonatomic, readonly, strong) UITabBarController *tabBarController; 

@end

NS_ASSUME_NONNULL_END

深思可得:

  • UITabBarController继承与UIViewContrller,所以就决定了UITabBarController的属性是对数组中的UIViewContrller进行设置的。
  • 当UITabBarController做为Window的根控制器时,程序一启动,UITabBarController就会一次性初始化所有子控制器,但是默认只加载第一个控制器视图,其他视图控制器只初始化,但默认不会加载,只有在需要显示的时候才调用loadView方法加载。
  • 每个视图控制器都有一个tabBarController属性,通过它可以访问所在的UITabBarController,而且对于UITabBarController的直接子视图,其tabBarController属性相当于它的父视图parentViewController

4.继承分析UITabBar

UItabBarController_第2张图片
072236159742491.png

下方的工具条称为UITabBar ,如果UITabBarController有N个子控制器,那么UITabBar内部就会有N 个UITabBarButton作为子控件与之对应。
注意:UITabBarButton在UITabBar中得位置是均分的,UITabBar的高度为49。

//
//  UITabBar.h
//  UIKit
//
//  Copyright (c) 2008-2016 Apple Inc. All rights reserved.
//

#import 
#import 
#import 

NS_ASSUME_NONNULL_BEGIN

typedef NS_ENUM(NSInteger, UITabBarItemPositioning) {
    UITabBarItemPositioningAutomatic,
    UITabBarItemPositioningFill,
    UITabBarItemPositioningCentered,
} NS_ENUM_AVAILABLE_IOS(7_0);

@class UITabBarItem;
@class UIImageView;
@protocol UITabBarDelegate;

NS_CLASS_AVAILABLE_IOS(2_0) @interface UITabBar : UIView

*****************属性********************
@property(nullable, nonatomic, weak) id delegate;     // weak reference. default is nil

// 设置UITabBarItem数组,默认为空
@property(nullable, nonatomic, copy) NSArray *items;     
  
//设置选中数据模型
@property(nullable, nonatomic, weak) UITabBarItem *selectedItem; 

//// 设置UITabBarItem数组
- (void)setItems:(nullable NSArray *)items animated:(BOOL)animated;   

// //将淡入或淡出或者重新排序和调整间距(动画效果)(学习)
- (void)beginCustomizingItems:(NSArray *)items __TVOS_PROHIBITED;  
- (BOOL)endCustomizingAnimated:(BOOL)animated __TVOS_PROHIBITED;   
#if UIKIT_DEFINE_AS_PROPERTIES
@property(nonatomic, readonly, getter=isCustomizing) BOOL customizing __TVOS_PROHIBITED;
#else
- (BOOL)isCustomizing __TVOS_PROHIBITED;
#endif

//  please use -barTintColor.
@property(null_resettable, nonatomic, strong) UIColor *tintColor NS_AVAILABLE_IOS(5_0);
// 7.0以后的,修改背景颜色
@property(nullable, nonatomic, strong) UIColor *barTintColor NS_AVAILABLE_IOS(7_0) UI_APPEARANCE_SELECTOR;  // default is nil


//修改未选中Item的字体颜色
@property (nonatomic, readwrite, copy, nullable) UIColor *unselectedItemTintColor NS_AVAILABLE_IOS(10_0) UI_APPEARANCE_SELECTOR;
//修改选中Item的字体颜色
@property(nullable, nonatomic, strong) UIColor *selectedImageTintColor NS_DEPRECATED_IOS(5_0,8_0,"Use tintColor") UI_APPEARANCE_SELECTOR __TVOS_PROHIBITED;

//设置UITabBar的背景图片
@property(nullable, nonatomic, strong) UIImage *backgroundImage NS_AVAILABLE_IOS(5_0) UI_APPEARANCE_SELECTOR;

// // 设置选中的按钮的图片
@property(nullable, nonatomic, strong) UIImage *selectionIndicatorImage NS_AVAILABLE_IOS(5_0) UI_APPEARANCE_SELECTOR;

// 设置阴影图片,但必须设置backgroundImage属性
@property(nullable, nonatomic, strong) UIImage *shadowImage NS_AVAILABLE_IOS(6_0) UI_APPEARANCE_SELECTOR;

// 设置定位属性
@property(nonatomic) UITabBarItemPositioning itemPositioning NS_AVAILABLE_IOS(7_0) UI_APPEARANCE_SELECTOR __TVOS_PROHIBITED;

//  设置Item的宽度
@property(nonatomic) CGFloat itemWidth NS_AVAILABLE_IOS(7_0) UI_APPEARANCE_SELECTOR;

// 设置Item间距
@property(nonatomic) CGFloat itemSpacing NS_AVAILABLE_IOS(7_0) UI_APPEARANCE_SELECTOR;

// 设置风格(学习)
@property(nonatomic) UIBarStyle barStyle NS_AVAILABLE_IOS(7_0) UI_APPEARANCE_SELECTOR __TVOS_PROHIBITED;

//  (学习)
@property(nonatomic,getter=isTranslucent) BOOL translucent NS_AVAILABLE_IOS(7_0);
@end

//___________________________________________________________________________________________________

@protocol UITabBarDelegate
@optional

//选中后调用
- (void)tabBar:(UITabBar *)tabBar didSelectItem:(UITabBarItem *)item; // called when a new view is selected by the user (but not programatically)

//  // 即将编辑时调用
- (void)tabBar:(UITabBar *)tabBar willBeginCustomizingItems:(NSArray *)items __TVOS_PROHIBITED;          
  
// 编辑时调用 
- (void)tabBar:(UITabBar *)tabBar didBeginCustomizingItems:(NSArray *)items __TVOS_PROHIBITED;          
   
// 即将结束编辑时调用   
- (void)tabBar:(UITabBar *)tabBar willEndCustomizingItems:(NSArray *)items changed:
(BOOL)changed __TVOS_PROHIBITED; 

// 结束编辑时调用
- (void)tabBar:(UITabBar *)tabBar didEndCustomizingItems:(NSArray *)items changed:(BOOL)changed __TVOS_PROHIBITED;  // called after customize sheet is hidden. items is new item list

@end
  • 1.UITabBar继承于UIVeiw;
  • 2.API中主要用来描述View的颜色和TabBarItem的属性调用;
  1. 继续分析tabBarItem
UItabBarController_第3张图片
是对UIBarItem的封装
//
//  UITabBarItem.h
//  UIKit
//
//  Copyright (c) 2008-2016 Apple Inc. All rights reserved.
//

#import 
#import 
#import 
#import 

NS_ASSUME_NONNULL_BEGIN
**********UITabBarSystemItem的枚举类型************
typedef NS_ENUM(NSInteger, UITabBarSystemItem) {
    UITabBarSystemItemMore,
    UITabBarSystemItemFavorites,
    UITabBarSystemItemFeatured,
    UITabBarSystemItemTopRated,
    UITabBarSystemItemRecents,
    UITabBarSystemItemContacts,
    UITabBarSystemItemHistory,
    UITabBarSystemItemBookmarks,
    UITabBarSystemItemSearch,
    UITabBarSystemItemDownloads,
    UITabBarSystemItemMostRecent,
    UITabBarSystemItemMostViewed,
};

@class UIView, UIImage;

NS_CLASS_AVAILABLE_IOS(2_0) @interface UITabBarItem : UIBarItem 

// (学习)
- (instancetype)init NS_DESIGNATED_INITIALIZER;
- (nullable instancetype)initWithCoder:(NSCoder *)aDecoder NS_DESIGNATED_INITIALIZER;

// 初始化UITabBarItem对象
- (instancetype)initWithTitle:(nullable NSString *)title image:(nullable UIImage *)image tag:(NSInteger)tag;

// 初始化UITabBarItem对象
- (instancetype)initWithTitle:(nullable NSString *)title image:(nullable UIImage *)image selectedImage:(nullable UIImage *)selectedImage NS_AVAILABLE_IOS(7_0);

//初始化系统UITabBarItem对象
- (instancetype)initWithTabBarSystemItem:(UITabBarSystemItem)systemItem tag:(NSInteger)tag;

// 选择的图片
@property(nullable, nonatomic,strong) UIImage *selectedImage NS_AVAILABLE_IOS(7_0);

//  徽章数
@property(nullable, nonatomic, copy) NSString *badgeValue;    // default is nil

// 设置选择结束后的图片
- (void)setFinishedSelectedImage:(nullable UIImage *)selectedImage withFinishedUnselectedImage:(nullable UIImage *)unselectedImage NS_DEPRECATED_IOS(5_0,7_0,"Use initWithTitle:image:selectedImage: or the image and selectedImage properties along with UIImageRenderingModeAlwaysOriginal") __TVOS_PROHIBITED;
- (nullable UIImage *)finishedSelectedImage NS_DEPRECATED_IOS(5_0,7_0) __TVOS_PROHIBITED;
- (nullable UIImage *)finishedUnselectedImage NS_DEPRECATED_IOS(5_0,7_0) __TVOS_PROHIBITED;

//指定相应的数据去偏移一个位置,向右或者向下为正值,向左或者向上为负值,不过首先你得有一个相对位置的坐标。而tabbarItem文字的坐标是底部为x轴,y轴则是tabbarItem的centerX; 
@property (nonatomic, readwrite, assign) UIOffset titlePositionAdjustment NS_AVAILABLE_IOS(5_0) UI_APPEARANCE_SELECTOR;

//徽章的颜色
@property (nonatomic, readwrite, copy, nullable) UIColor *badgeColor NS_AVAILABLE_IOS(10_0) UI_APPEARANCE_SELECTOR;

//设置徽章的属性
- (void)setBadgeTextAttributes:(nullable NSDictionary *)textAttributes forState:(UIControlState)state NS_AVAILABLE_IOS(10_0) UI_APPEARANCE_SELECTOR;

- (nullable NSDictionary *)badgeTextAttributesForState:(UIControlState)state NS_AVAILABLE_IOS(10_0) UI_APPEARANCE_SELECTOR;

@end
NS_ASSUME_NONNULL_END

深思得:

  • UITabBarItem封装了UIBarItem;
  • UITabBarItem的API主要是对封装好的标题,图片,徽章进行赋值和简单点的改变颜色等

6.继续分析UIBarItem

UItabBarController_第4张图片
42E526CA-0DBB-45DF-BEC9-8B3C67382BA6.png
//
//  UIBarItem.h
//  UIKit
//
//  Copyright (c) 2008-2015 Apple Inc. All rights reserved.
//

#import 
#import 
#import 
#import 
#import 

NS_ASSUME_NONNULL_BEGIN

@class UIImage;

NS_CLASS_AVAILABLE_IOS(2_0) @interface UIBarItem : NSObject 

- (instancetype)init NS_DESIGNATED_INITIALIZER;
- (nullable instancetype)initWithCoder:(NSCoder *)aDecoder NS_DESIGNATED_INITIALIZER;
 // 是否有效
@property(nonatomic,getter=isEnabled) BOOL         enabled;      // default is YES

// 设置标题
@property(nullable, nonatomic,copy)             NSString    *title;        // default is nil

// 设置图片
@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

// 设置图片边距
@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.
 */
- (void)setTitleTextAttributes:(nullable NSDictionary *)attributes forState:(UIControlState)state NS_AVAILABLE_IOS(5_0) UI_APPEARANCE_SELECTOR;
- (nullable NSDictionary *)titleTextAttributesForState:(UIControlState)state NS_AVAILABLE_IOS(5_0) UI_APPEARANCE_SELECTOR;

@end

NS_ASSUME_NONNULL_END

深思得:

  • UIBarItem 继承于NSObject。
  • 由上API可猜的UIBarItem是由Nsstring,Image 组成的。
  • 由于UITabBarItem封装了UIBarItem,所以可以直接在UITabBarItem调用UIBarItem的属性进行设置即可。

7.献上结构

UItabBarController_第5张图片
F37DBCD4-6AD1-4FD3-A478-0E540088C09B.png

8.日常代码

标签视图

SeconViewController *second = [SeconViewController new];
UINavigationController *secondNC = [[UINavigationController alloc] initWithRootViewController:second];
//第二种初始化方式(TabBar自定义样式);
//默认状态下显示的图片
UIImage *secondNCimage = [UIImage imageNamed:@"carGary"];
//选中状态下显示的图片
UIImage *secondSelectedImage = [UIImage imageNamed:@"carRed"];

//图片不被渲染,保持原图
secondNCimage = [secondNCimage imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
secondSelectedImage = [secondSelectedImage  imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
secondNC.tabBarItem = [[UITabBarItem alloc] initWithTitle:@"second" image:secondNCimage selectedImage:secondSelectedImage];

创建UITabBarController

//1:创建一个UITabBarController(高:49)
UITabBarController *mainTabBar = [UITabBarController new];
//2:设置TabBarController的子视图控制器
mainTabBar.viewControllers = @[firstNC,secondNC,thirdNC];
//3:将根视图控制器设置为:TabBarController
[self.window setRootViewController:mainTabBar];
//设置tabBar选中时title的颜色(如果:TabBarItem是系统默认的样式,则设置该属性后,图标和文字同时改变颜色)
[mainTabBar.tabBar setTintColor:[UIColor redColor]];
//设置tabBar背景颜色
[mainTabBar.tabBar setBarTintColor:[UIColor colorWithRed:0.000 green:0.039 blue:1.000 alpha:1.000]];
//改变tabBar的位置
//[secondNC.tabBarItem setTitlePositionAdjustment:UIOffsetMake(30, 30)];
//设置进入程序后默认选中第几个
mainTabBar.selectedIndex = 0;
//设置提示信息
firstNC.tabBarItem.badgeValue = @"点我";
secondNC.tabBarItem.badgeValue = @"99+";
thirdNC.tabBarItem.badgeValue = @"放大";
//设置代理人
mainTabBar.delegate = self;

你可能感兴趣的:(UItabBarController)