2017-02-22 49 views
1

我想根據用戶的操作在文件顏色加載後更改UITabBarControllers文本顏色。是否可以在加載後更改UITabBarController文本顏色?

我可以調用它,它會在啓動時正確更改字體和顏色。

let appearance = UITabBarItem.appearance() 
let attributes: [String: AnyObject] = [NSFontAttributeName:UIFont(name: "American Typewriter", size: 12)!, NSForegroundColorAttributeName: UIColor.green] 
appearance.setTitleTextAttributes(attributes, for: .normal) 

當我嘗試在應用程序完全啓動後的任何時候調用此函數時,沒有任何反應。

這是可能的嗎,還是啓動時可以做的一次性事情?

+0

你可以用'UITabBarItem.appearance()setTitleTextAttributes檢查。([NSForegroundColorAttributeName:UIColor.redColor()],forState:.Selected)'或正常狀態做'.Normal' –

+0

或者用'tabBarController檢查.tabBar' tintColor –

+0

你應該試試這個 - http://stackoverflow.com/questions/31117069/changing-tab-bar-item-image-and-text-color-ios –

回答

0

它看起來像是如果我遍歷所有的項目,並設置它們手動它的工作。然後我需要重新設置所選項目的顏色。

還不確定爲什麼其他方式不起作用?

for myItem in (tabBar.items)! { 
    myItem.setTitleTextAttributes([NSForegroundColorAttributeName: UIColor.red], for: .normal) 
} 

tabBar.selectedItem?.setTitleTextAttributes([NSForegroundColorAttributeName: UIColor.green], for: .normal)