[英]Change UINavigationBar Background Color of UISearchController
[英]How to change UINavigationBar background color from the AppDelegate
我知道如何通過執行更改UINavigationBar
背景圖像
[[UINavigationBar appearance] setBackgroundImage:[UIImage imageNamed:@"nabbar"] forBarMetrics:UIBarMetricsDefault];
而且我知道如何在每個Views
中將欄設置為不同的顏色......現在我想在不使用圖像的情況下將背景顏色更改為app delegate
的純色。 我不想每次都從每個視圖設置它,我不想寫一個CGRect
。
我試過[[UINavigationBar appearance] setBackgroundColor:[UIColor colorWithRed:33/255.0 green:34/255.0 blue:36/255.0 alpha:1.0]];
但我不工作,我無法在應用程序委托中的任何地方找到代碼。
有人可以指出我正確的方向嗎?
你可以使用[[UINavigationBar appearance] setTintColor:myColor];
從iOS 7開始,你需要設置[[UINavigationBar appearance] setBarTintColor:myColor];
還有[[UINavigationBar appearance] setTranslucent:NO]
。
[[UINavigationBar appearance] setBarTintColor:myColor];
[[UINavigationBar appearance] setTranslucent:NO];
要更改背景顏色而不是色調,以下代碼將起作用:
[self.navigationController.navigationBar setBarTintColor:[UIColor greenColor]];
[self.navigationController.navigationBar setTranslucent:NO];
要在iOS 7中執行此操作:
[[UINavigationBar appearance] setBarTintColor:myColor];
Swift語法:
UINavigationBar.appearance().barTintColor = UIColor.whiteColor() //changes the Bar Tint Color
我只是把它放在AppDelegate didFinishLaunchingWithOptions中,它在整個應用程序中都存在
您可以使用Xcode 6.3.1輕松完成此操作。 在文檔大綱中選擇您的NavigationBar。 選擇“屬性”檢查器。 取消選中半透明。 將Bar Tint設置為所需的顏色。 完成!
斯威夫特 :
self.navigationController?.navigationBar.barTintColor = UIColor.red
self.navigationController?.navigationBar.isTranslucent = false
正如其他答案所提到的,你可以使用setTintColor:
,但你想要一個純色,並且不可能設置色調顏色AFAIK。
解決方案是以編程方式創建圖像,並通過UIAppearance
圖像設置為所有導航欄的背景圖像。 關於圖像的大小,我不確定1x1像素圖像是否可行或者您是否需要導航欄的確切大小。檢查此問題的第二個答案以了解如何創建圖像。
作為一個建議,我不喜歡用這些類型的東西“重載”應用程序委托。 我傾向於創建一個名為AppearanceConfiguration
的類,只有一個公共方法configureAppearance
,我在其中設置了我想要的所有UIAppearance,然后我從app delegate調用該方法。
iOS 13.0為此引入了新的 API:
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {
let myColor = UIColor(hue: 0.4, saturation: 0.25, brightness: 1, alpha: 1)
let barAppearance = UINavigationBarAppearance()
barAppearance.backgroundColor = myColor
let navigationBar = UINavigationBar.appearance()
navigationBar.standardAppearance = barAppearance
navigationBar.scrollEdgeAppearance = barAppearance // for scrollable content or large titles
return true
}
您可以在任何視圖控制器中使用此代碼設置UINavigation背景顏色
self.navigationController.navigationBar.backgroundColor = [UIColor colorWithRed:10.0f/255.0f green:30.0f/255.0f blue:200.0f/255.0f alpha:1.0f];
在Swift 4.2和Xcode 10.1中
您可以將AppDelegate中的導航欄顏色直接更改為整個項目。
在didFinishLaunchingWithOptions launchOptions:
寫下面的代碼行
UINavigationBar.appearance().tintColor = UIColor.white
UINavigationBar.appearance().barTintColor = UIColor(red: 2/255, green: 96/255, blue: 130/255, alpha: 1.0)
這里
tintColor用於設置背景圖像,如后退按鈕和菜單行圖像等。(見下左右菜單圖像)
barTintColor用於導航欄背景顏色
如果要設置特定的視圖控制器導航欄顏色,請在viewDidLoad()
寫下面的代碼
//Add navigation bar colour
navigationController?.navigationBar.barTintColor = UIColor(red: 2/255, green: 96/255, blue: 130/255, alpha: 1.0)
navigationController?.navigationBar.tintColor = UIColor.white
在 iOS 中處理 UINavigationController 和 UINavigationBar 很麻煩。 好在有開源的第三方庫可以輕松解決這些問題,希望對大家有所幫助。
Git 存儲庫: NXNavigationExtension
更改 UINavigationBar 顏色:
extension YourViewController {
override var nx_titleTextAttributes: [NSAttributedString.Key : Any]? {
return [NSAttributedString.Key.foregroundColor: .red]
}
}
📝 例子
顏色代碼是這里的問題。 而不是使用195/255,使用0.7647或195.f / 255.f問題是轉換浮動不能正常工作。 嘗試使用精確浮點值。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.