??? UINavigationBar可以通過設(shè)置tintColor來改變導(dǎo)航條的背景顏色,但是由于UIBarButtonItem沒有文本顏色設(shè)置功能,所以如果將UINavigationBar的tintColor設(shè)置成whiteColor的話,文字顯示就不怎么清晰了。
??? 網(wǎng)上一般建議通過建立一個(gè)UILabel,賦值給UINavigationItem的titleView屬性,改變標(biāo)題的顏色。建立一個(gè)UIButton,通過UIBarButtonItem的initWithCustomView方法創(chuàng)建UIBarButtonItem對象
效果不盡人意吧。當(dāng)然可以通過設(shè)置背景圖片什么的,加強(qiáng)效果。但總體來說不如只改變文本顏色方便。
iOS的Objective C提供了runtime函數(shù),定義在objc目錄下面。通過這些運(yùn)行時(shí)庫函數(shù)可以對系統(tǒng)定義的對象進(jìn)行修改,比如增加方法,修改方法的代碼地址....通過枚舉UINavigationBar的子視圖,發(fā)現(xiàn)顯示UIBarButtonItem內(nèi)容的是UINavigationButton,它有一個(gè)子視圖類型為UIButtonLabel,UIButtonLabel繼承自UILabel,UIButtonLabel類型本身沒有重載setTextColor:方法,因此調(diào)用class_addMethod給UIButtonLabel類型增加一個(gè)setTextColor:方法,然后把傳進(jìn)來的color強(qiáng)制改成其他顏色,再調(diào)用UILabel的setTextColor:方法即可。