[英]How to change color for Navigation Bar title?
我有一個簡單的導航欄,以及我通過情節提要設置的標題。 我為標題設置了白色。
然后我增加了尺寸。
navigationController?.navigationBar.prefersLargeTitles = true
這會自動更改標題的顏色/字體和所有屬性。
我嘗試了幾種方法(通常更改所有屬性),但是在增大尺寸后,似乎沒有一種方法可以解決此問題。
在這種情況下如何自定義標題?
注意* :我詢問有關特定NavVC / VC標題的顏色。 不在所有VC的AppDelegate中。
答:請按照下列步驟操作: 當“ prefersLargeTitles”設置為true時,更改導航欄標題的文本顏色
使用以下代碼更改顏色
UINavigationBar.appearance().titleTextAttributes = [NSAttributedStringKey(rawValue: NSAttributedStringKey.foregroundColor.rawValue): UIColor.orange]
如果另一個不工作,請嘗試這樣。
if #available(iOS 11.0, *) {
//To change iOS 11 navigationBar largeTitle color
UINavigationBar.appearance().prefersLargeTitles = true
UINavigationBar.appearance().largeTitleTextAttributes = [NSAttributedStringKey(rawValue: NSAttributedStringKey.foregroundColor.rawValue): UIColor.white]
} else {
// for default navigation bar title color
UINavigationBar.appearance().titleTextAttributes = [NSAttributedStringKey(rawValue: NSAttributedStringKey.foregroundColor.rawValue): UIColor.white]
}
請將此代碼添加到viewDidLoad()中以更改標題的顏色/字體。
if #available(iOS 11.0, *) {
UINavigationBar.appearance().largeTitleTextAttributes =
[NSAttributedStringKey.foregroundColor: UIColor.green,
NSAttributedStringKey.font:UIFont.boldSystemFont(ofSize: 20)]
} else {
// Fallback on earlier versions
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.