歡迎來到Linux教程網
Linux教程網
Linux教程網
Linux教程網
Linux教程網 >> Linux編程 >> Linux編程 >> iOS開發中標簽控制器的使用——UITabBarController

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

日期:2017/3/1 9:20:15   编辑:Linux編程

一、引言

與導航控制器相類似,標簽控制器也是用於管理視圖控制器的一個UI控件,在其內部封裝了一個標簽欄,與導航不同的是,導航的管理方式是縱向的,采用push與pop切換控制器,標簽的管理是橫向的,通過標簽的切換來改變控制器,一般我們習慣將tabBar作為應用程序的根視圖控制器,在其中添加導航,導航中在對ViewController進行管理。

二、創建一個標簽控制器

通過如下的步驟,我們可以很簡便的創建一個TabBarController:

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的導航,並且可以通過系統自帶的編輯來調整控制器的順序,如下:

三、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;


四、關於標簽欄TabBar

通過自定義標簽欄的一些屬性,使我們可以更加靈活的使用tabBar。

1、UITabBar屬性和方法

設置標簽:

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

設置自定義標簽順序:

//調用這個方法會彈出一個類似上面第二張截圖的控制器,我們可以交換標簽的布局順序
- (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;

設置背景圖案:

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

TabBar中標簽的宏觀屬性:

//設置標簽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;

2、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;

五、再看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;

Copyright © Linux教程網 All Rights Reserved