設定 TabBarItem 選中時的圖片及文字顏色,

來源:互聯網
上載者:User

設定 TabBarItem 選中時的圖片及文字顏色,

TabBarController 是在 ios 開發過程中使用較為頻繁的一個 Controller,但是在使用過程中經常會遇到一些問題,例如本文所要解決的,如何修改 TabBar 選中時文字及圖片的顏色。

如果需要,可以從這裡下載完整的代碼。

首先,我們建立一個新的項目,使用 TabBarController,並為該 TabBarController 的 Child Controller 中的 TabBarItem 設定預設圖片以及選中時的圖片,如所示:

storyboard:

值意義UIImageRenderingModeAutomatic 根據圖片的使用位置自動調整渲染模式(預設值)UIImageRenderingModeAlwaysOriginal 始終繪製圖片原始狀態,不適用tint color。  UIImageRenderingModeAlwaysTemplate 使用根據tint color繪製圖片,忽略圖片的顏色資訊

所以要解決我們遇到的問題,只需要設定選中的圖片的render mode就可以了。

開啟其中一個view controller, 在viewDidLoad方法裡添加下述代碼:

1 UIImage *image = [self.tabBarItem.selectedImage imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];2 self.tabBarItem.selectedImage = image;View Code

重新運行程式,可以看到選中的tab bar item變成紅色的圖片了,但是文字的顏色還是木有變呀~

我們可以用 text attribute 的方式設定文字樣式,在上述代碼下繼續添加:

1 [self.tabBarItem setTitleTextAttributes:@{NSForegroundColorAttributeName: [UIColor redColor]} forState:UIControlStateSelected];View Code

 編譯運行,可以看到字和圖片都變成我們想要的啦,大功告成~

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.