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

來源:互聯網
上載者:User

標籤:

本文

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

一、引言

        與導航控制器相類似,標籤控制器也是用於管理檢視控制器的一個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;

 

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

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.