iOS開發中標籤控制器的使用——UITabBarController

iOS開發中標籤控制器的使用——UITabBarController

1、引言

        與導航控制器相相似,標籤控制器也是用於管理視圖控制器的一個UI控件,在其內部封裝了一個標籤欄,與導航不一樣的是,導航的管理方式是縱向的,採用push與pop切換控制器,標籤的管理是橫向的,經過標籤的切換來改變控制器,通常咱們習慣將tabBar做爲應用程序的根視圖控制器,在其中添加導航,導航中在對ViewController進行管理。數組

2、建立一個標籤控制器

        經過以下的步驟,咱們能夠很簡便的建立一個TabBarController:dom

UITabBarController * tabBar= [[UITabBarController alloc]init];
    NSMutableArray * controllerArray = [[NSMutableArray alloc]init];
    for (int i=0; i<4; i++) {
        UIViewController * con = [[UIViewController alloc]init];
        [con loadViewIfNeeded];
        con.view.backgroundColor = [UIColor colorWithRed:arc4random()%255/255.0 green:arc4random()%255/255.0 blue:arc4random()%255/255.0 alpha:1];
        con.tabBarItem.image = [UIImage imageNamed:@"btn_publish_face_a.png"];
        con.tabBarItem.title=[NSString stringWithFormat:@"%d",i+1];
        con.title = [NSString stringWithFormat:@"%d",i+1];
        [controllerArray addObject:con];
    }
    tabBar.viewControllers = controllerArray;
    [self presentViewController:tabBar animated:YES completion:nil];

經過點擊下面的標籤按鈕,能夠很方便的切換控制器。若是咱們的控制器數超過4個,系統會被咱們建立一個more的導航,而且能夠經過系統自帶的編輯來調整控制器的順序,以下:佈局

 

       

3、UITabBarController的屬性和方法

//管理的viewController數組
@property(nullable, nonatomic,copy) NSArray<__kindof UIViewController *> *viewControllers;
- (void)setViewControllers:(NSArray<__kindof UIViewController *> * __nullable)viewControllers animated:(BOOL)animated;
//選中的ViewControlle
@property(nullable, nonatomic, assign) __kindof UIViewController *selectedViewController;
//經過編號設置選中ViewController
@property(nonatomic) NSUInteger selectedIndex;
//當viewController大於4個時,獲取"更多"標籤的導航控制器
@property(nonatomic, readonly) UINavigationController *moreNavigationController; 
//這個屬性設置的是能夠進行自定義排列順序的視圖控制器,如上面第二張圖中的,默認是所有
@property(nullable, nonatomic, copy) NSArray<__kindof UIViewController *> *customizableViewControllers;
//標籤控制器中分裝的標籤欄
@property(nonatomic,readonly) UITabBar *tabBar NS_AVAILABLE_IOS(3_0);
//代理
@property(nullable, nonatomic,weak) id<UITabBarControllerDelegate> delegate;

4、關於標籤欄TabBar

        經過自定義標籤欄的一些屬性,使咱們能夠更加靈活的使用tabBar。字體

一、UITabBar屬性和方法

設置標籤:atom

@property(nullable,nonatomic,copy) NSArray<UITabBarItem *> *items;  
//設置選中的標籤    
@property(nullable,nonatomic,assign) UITabBarItem *selectedItem; 
- (void)setItems:(nullable NSArray<UITabBarItem *> *)items animated:(BOOL)animated;

設置自定義標籤順序:spa

//調用這個方法會彈出一個相似上面第二張截圖的控制器,咱們能夠交換標籤的佈局順序
- (void)beginCustomizingItems:(NSArray<UITabBarItem *> *)items;  
//完成標籤佈局
- (BOOL)endCustomizingAnimated:(BOOL)animated;   
//是否正在自定義標籤佈局
- (BOOL)isCustomizing;

設置tabBar顏色相關:代理

//設置渲染顏色,會影響選中字體和圖案的渲染
@property(null_resettable, nonatomic,strong) UIColor *tintColor;
//設置導航欄的顏色
@property(nullable, nonatomic,strong) UIColor *barTintColor;

設置背景圖案:code

//設置導航欄背景圖案
@property(nullable, nonatomic,strong) UIImage *backgroundImage;
//設置選中一個標籤時,標籤背後的選中提示圖案 這個會出如今設置的item圖案的後面
@property(nullable, nonatomic,strong) UIImage *selectionIndicatorImage;
//設置陰影的背景圖案
@property(nullable, nonatomic,strong) UIImage *shadowImage

TabBar中標籤的宏觀屬性:orm

//設置標籤item的位置模式
@property(nonatomic) UITabBarItemPositioning itemPositioning;
//枚舉以下
typedef NS_ENUM(NSInteger, UITabBarItemPositioning) {
    UITabBarItemPositioningAutomatic,//自動
    UITabBarItemPositioningFill,//充滿
    UITabBarItemPositioningCentered,//中心
} NS_ENUM_AVAILABLE_IOS(7_0);
//設置item寬度
@property(nonatomic) CGFloat itemWidth;
//設置item間距
@property(nonatomic) CGFloat itemSpacing;

與導航欄相似,也能夠設置tabBar的風格和透明效果:繼承

//風格 分黑白兩種
@property(nonatomic) UIBarStyle barStyle;
//是否透明效果
@property(nonatomic,getter=isTranslucent) BOOL translucent;

二、UITabBarDelegate

//選中標籤時調用
- (void)tabBar:(UITabBar *)tabBar didSelectItem:(UITabBarItem *)item;
//將要開始編輯標籤時
- (void)tabBar:(UITabBar *)tabBar willBeginCustomizingItems:(NSArray<UITabBarItem *> *)items;          //已經開始編輯標籤時         
- (void)tabBar:(UITabBar *)tabBar didBeginCustomizingItems:(NSArray<UITabBarItem *> *)items;           
//將要進入編輯狀態時
- (void)tabBar:(UITabBar *)tabBar willEndCustomizingItems:(NSArray<UITabBarItem *> *)items changed:(BOOL)changed; 
//已經進入編輯狀態時
- (void)tabBar:(UITabBar *)tabBar didEndCustomizingItems:(NSArray<UITabBarItem *> *)items changed:(BOOL)changed;

5、再看UITabBarItem

        和NavigationItem相似,標籤欄上的item也能夠自定義,一些方法以下。

初始化方法:

//經過標題和圖案進行建立
- (instancetype)initWithTitle:(nullable NSString *)title image:(nullable UIImage *)image tag:(NSInteger)tag;
- (instancetype)initWithTitle:(nullable NSString *)title image:(nullable UIImage *)image selectedImage:(nullable UIImage *)selectedImage;
//建立系統類型的
- (instancetype)initWithTabBarSystemItem:(UITabBarSystemItem)systemItem tag:(NSInteger)tag;

UITabBarSystemItem的枚舉以下:

typedef NS_ENUM(NSInteger, UITabBarSystemItem) {
    UITabBarSystemItemMore,//更多圖標
    UITabBarSystemItemFavorites,//最愛圖標
    UITabBarSystemItemFeatured,//特徵圖標
    UITabBarSystemItemTopRated,//高級圖標
    UITabBarSystemItemRecents,//最近圖標
    UITabBarSystemItemContacts,//聯繫人圖標
    UITabBarSystemItemHistory,//歷史圖標
    UITabBarSystemItemBookmarks,//圖書圖標
    UITabBarSystemItemSearch,//查找圖標
    UITabBarSystemItemDownloads,//下載圖標
    UITabBarSystemItemMostRecent,//記錄圖標
    UITabBarSystemItemMostViewed,//所有查看圖標
};

UITabBarItem經常使用屬性:

//設置選中圖案
@property(nullable, nonatomic,strong) UIImage *selectedImage;

下面這個屬性能夠設置item的頭標文字:

 con.tabBarItem.badgeValue = @"1";

//設置標題的位置偏移
@property (nonatomic, readwrite, assign) UIOffset titlePositionAdjustment;

因爲UITabBarItem是繼承於UIBarItem,還有下面這個屬性能夠設置使用:

//標題
@property(nullable, nonatomic,copy)             NSString    *title;   
//圖案    
@property(nullable, nonatomic,strong)           UIImage     *image;  
//橫屏時的圖案      
@property(nullable, nonatomic,strong)           UIImage     *landscapeImagePhone;
//圖案位置偏移
@property(nonatomic)                  UIEdgeInsets imageInsets; 
//橫屏時的圖案位置偏移
@property(nonatomic)                  UIEdgeInsets landscapeImagePhoneInsets ;
//設置和獲取標題的字體屬性
- (void)setTitleTextAttributes:(nullable NSDictionary<NSString *,id> *)attributes forState:(UIControlState)state;
- (nullable NSDictionary<NSString *,id> *)titleTextAttributesForState:(UIControlState)state;

專一技術,熱愛生活,交流技術,也作朋友。

——琿少 QQ羣:203317592

相關文章
相關標籤/搜索