iOS開(kāi)發(fā) - UITabBarController 標(biāo)簽欄的設(shè)置

1.每個(gè)UIViewController都有一個(gè)tabBarItem

@interface UIViewController (UITabBarControllerItem)

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

2.UITabBarItem(子類(lèi))

NS_CLASS_AVAILABLE_IOS(2_0) @interface UITabBarItem : UIBarItem

3.UIBarItem(父類(lèi))

NS_CLASS_AVAILABLE_IOS(2_0) @interface UIBarItem : NSObject <NSCoding, UIAppearance>
// 設(shè)置tabBarItem的文字
@property(nullable, nonatomic,copy)             NSString    *title;        // default is nil

4.示例代碼

  • 示例代碼1
//創(chuàng)建子控制器
UIViewController *c1=[[UIViewController alloc]init];
c1.view.backgroundColor=[UIColor grayColor];
c1.view.backgroundColor=[UIColor greenColor];
c1.tabBarItem.title=@"消息";
c1.tabBarItem.image=[UIImage imageNamed:@"tab_recent_nor"];
c1.tabBarItem.badgeValue=@"123";
  • 示例代碼2
/**
 往tabbar添加一個(gè)子控制器

 @param childController 子控制器
 @param title 子控制器的title
 @param image tabbar的圖片名字
 */
- (void)addChildViewController:(UIViewController *)childController title: (NSString *)title image: (NSString *)image {
    childController.title = title;
    
    // 設(shè)置tabbarItem的文字大小和顏色
    [childController.tabBarItem setTitleTextAttributes:@{NSFontAttributeName: [UIFont systemFontOfSize:11], NSForegroundColorAttributeName: globalColor} forState:UIControlStateSelected];
    [childController.tabBarItem setTitleTextAttributes:@{NSFontAttributeName: [UIFont systemFontOfSize:11], NSForegroundColorAttributeName: [UIColor darkGrayColor]} forState:UIControlStateNormal];
    
    // 設(shè)置tabbar的圖片
    // 如果沒(méi)有傳圖片(中間的占位控制器), 就不設(shè)置tabbar的image
    if (image != nil) {
        childController.tabBarItem.image = [[UIImage imageNamed:[NSString stringWithFormat:@"tabbar_%@", image]] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
        childController.tabBarItem.selectedImage = [[UIImage imageNamed:[NSString stringWithFormat:@"tabbar_%@_selected", image]] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
    }
    
    UINavigationController *navController = [[UINavigationController alloc] initWithRootViewController:childController];
    
    [self addChildViewController:navController];
}

  • 示例代碼3

// 設(shè)置tabbarItem的文字大小和顏色
[childController.tabBarItem setTitleTextAttributes:@{NSFontAttributeName: [UIFont systemFontOfSize:11], NSForegroundColorAttributeName: globalColor} forState:UIControlStateSelected];
[childController.tabBarItem setTitleTextAttributes:@{NSFontAttributeName: [UIFont systemFontOfSize:11], NSForegroundColorAttributeName: [UIColor darkGrayColor]} forState:UIControlStateNormal];

  • 很多屬性的設(shè)置都可以在這個(gè)文件里找到

NSAttributedString.h

5.添加子控制器到UITabBarController中

  • 方式一
[tb addChildViewController:c1];
[tb addChildViewController:c2];
  • 方式二
tb.viewControllers=@[c1,c2,c3,c4];

6.UITabBar
下方的工具條稱為UITabBar ,如果UITabBarController有N個(gè)子控制器,那么UITabBar內(nèi)部就會(huì)有N 個(gè)UITabBarButton作為子控件與之對(duì)應(yīng)。

注意:UITabBarButton在UITabBar中得位置是均分的,UITabBar的高度為49

UITabBar
UITabBarItem的設(shè)置
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書(shū)系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容