自定義tabBarItem中的圖片和文字樣式

設置每個tabBarItem的圖片和文字需要到各自的視圖控制器下實現

具體用到以下方法 (在UIKit框架下UIBarItem.h文件裏)



/* 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:(NSDictionary *)attributes forState:(UIControlState)state NS_AVAILABLE_IOS(5_0) UI_APPEARANCE_SELECTOR;
- (NSDictionary *)titleTextAttributesForState:(UIControlState)state NS_AVAILABLE_IOS(5_0) UI_APPEARANCE_SELECTOR;


在每個tabBarItem所在的控制器中寫入如下代碼


// tabBarItem 點擊文字或者高亮的時候的顏色
    [self.tabBarItem setTitleTextAttributes:[NSDictionary dictionaryWithObjectsAndKeys:[UIColor redColor],UITextAttributeTextColor, nil] forState:UIControlStateHighlighted];
    // tabBarItem 文字失去焦點或者默認的時候的顏色
    [self.tabBarItem setTitleTextAttributes:[NSDictionary dictionaryWithObjectsAndKeys:[[UIColor alloc] initWithHue:0 saturation:0 brightness:0.6 alpha:1],UITextAttributeTextColor, nil] forState:UIControlStateNormal];
    // tabBarItem圖片的默認選中圖片
    [self.tabBarItem setImage:[UIImage imageNamed:@"tabar_bg_liuxue_hover.png"]];
    // tabBarItem 設置完成選中後的圖片和失去焦點後的圖片背景
    [self.tabBarItem setFinishedSelectedImage:[UIImage imageNamed:@"tabar_bg_liuxue_hover.png"] withFinishedUnselectedImage:[UIImage imageNamed:@"tabar_bg_liuxue.png"]];

實現的效果如圖所示








發佈了16 篇原創文章 · 獲贊 3 · 訪問量 6萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章