簡體   English   中英

更改 iOS 導航欄的顏色

[英]Change the color of iOS Navigation Bar

我正在嘗試更改導航欄的顏色,但我發現如果導航欄是根導航欄,這是不可能的。

我正在嘗試這個:

self.navigationController?.navigationBar.translucent = true

self.navigationController!.navigationBar.barTintColor = UIColor.blueColor()

我所有的Viewcontrollers都與導航器控制器相關。 然而什么都沒有改變。 事實上,我試圖從故事板中制作相同的東西,但只有當我在第一個導航器中時它才有效。

我試圖閱讀與此問題相關的所有內容,但一無所獲

我可以像這樣向導航欄添加任何項目

let HomeImage = UIImage(named: "home")!
    let Home : UIBarButtonItem = UIBarButtonItem(image: HomeImage,  style: .Plain, target: self, action: "home:")
    navigationItem.rightBarButtonItem = Home

事實上,我發現解決方案是在AppDelegate.siwft使用:

func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool {
    // Override point for customization after application launch.
    UINavigationBar.appearance().barTintColor = UIColor(red: 0, green: 0/255, blue: 205/255, alpha: 1)
    UINavigationBar.appearance().tintColor = UIColor.whiteColor()
    UINavigationBar.appearance().titleTextAttributes = [NSForegroundColorAttributeName:UIColor.whiteColor()]

    return true
}

然后在每個視圖控制器中,我們需要另一種背景顏色或其他顏色

  1. segue 應該與“show”不同

  2. 使用 func viewWillAppear

     override func viewWillAppear(animated: Bool) { super.viewWillAppear(animated) self.navigationController?.navigationBar.barTintColor = UIColor.whiteColor() self.navigationController?.navigationBar.tintColor = UIColor.blueColor() self.navigationController!.navigationBar.titleTextAttributes = [NSForegroundColorAttributeName: UIColor.blueColor()] }

為 Swift 3 更新

    UINavigationBar.appearance().barTintColor = .black
    UINavigationBar.appearance().tintColor = .white
    UINavigationBar.appearance().titleTextAttributes = [NSForegroundColorAttributeName: UIColor.white]
    UINavigationBar.appearance().isTranslucent = false

斯威夫特 4

    UINavigationBar.appearance().barTintColor = .black
    UINavigationBar.appearance().tintColor = .white
    UINavigationBar.appearance().titleTextAttributes = [NSAttributedStringKey.foregroundColor: UIColor.white]
    UINavigationBar.appearance().isTranslucent = false

斯威夫特 5

    UINavigationBar.appearance().barTintColor = .black
    UINavigationBar.appearance().tintColor = .white
    UINavigationBar.appearance().titleTextAttributes = [NSAttributedString.Key.foregroundColor: UIColor.white]
    UINavigationBar.appearance().isTranslucent = false

斯威夫特 4.2

    //To change Navigation Bar Background Color
    UINavigationBar.appearance().barTintColor = UIColor.blue
    //To change Back button title & icon color
    UINavigationBar.appearance().tintColor = UIColor.white
    //To change Navigation Bar Title Color
    UINavigationBar.appearance().titleTextAttributes = [NSAttributedString.Key.foregroundColor: UIColor.white]

斯威夫特 3.x

//To change Navigation Bar Background Color
UINavigationBar.appearance().barTintColor = UIColor.blue
//To change Back button title & icon color
UINavigationBar.appearance().tintColor = UIColor.white
//To change Navigation Bar Title Color
UINavigationBar.appearance().titleTextAttributes = [NSForegroundColorAttributeName: UIColor.white]

要在整個應用程序中更改導航欄主題顏色,您可以使用 UiNavigation 欄外觀來完成此操作。

UINavigationBar.appearance().barTintColor = UIColor.redColor()

如果您的視圖控制器嵌入在導航控制器中,那么您可以刪除此默認導航欄,並可以為該視圖控制器使用自定義導航欄。

然后你可以看起來像

UINavigationBar.appearance().barTintColor = UIColor(red: 46.0/255.0, green: 14.0/255.0, blue: 74.0/255.0, alpha: 1.0)

對 AppDelegate.Swift 文件進行以下更新,即UINavigationBar.appearance().barTintColor = UIColor(red:x.xx, green:x.xx, blue:x.xx, alpha:1.0)

參考下面的例子

import UIKit

@UIApplicationMain
class AppDelegate: UIResponder, UIApplicationDelegate {

var window: UIWindow?


func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {
    // Override point for customization after application launch.
    UINavigationBar.appearance().barTintColor = UIColor(red:0.03, green:0.25, blue:0.11, alpha:1.0)
    UINavigationBar.appearance().tintColor = UIColor.white
    UINavigationBar.appearance().titleTextAttributes = [NSForegroundColorAttributeName:UIColor.white]
    return true
}

func applicationWillResignActive(_ application: UIApplication) {
    // Sent when the application is about to move from active to inactive state. This can occur for certain types of temporary interruptions (such as an incoming phone call or SMS message) or when the user quits the application and it begins the transition to the background state.
    // Use this method to pause ongoing tasks, disable timers, and invalidate graphics rendering callbacks. Games should use this method to pause the game.
}

func applicationDidEnterBackground(_ application: UIApplication) {
    // Use this method to release shared resources, save user data, invalidate timers, and store enough application state information to restore your application to its current state in case it is terminated later.
    // If your application supports background execution, this method is called instead of applicationWillTerminate: when the user quits.
}

func applicationWillEnterForeground(_ application: UIApplication) {
    // Called as part of the transition from the background to the active state; here you can undo many of the changes made on entering the background.
}

func applicationDidBecomeActive(_ application: UIApplication) {
    // Restart any tasks that were paused (or not yet started) while the application was inactive. If the application was previously in the background, optionally refresh the user interface.
}

func applicationWillTerminate(_ application: UIApplication) {
    // Called when the application is about to terminate. Save data if appropriate. See also applicationDidEnterBackground:.
}

}
self.navigationController?.navigationBar.barTintColor = UIColor.gray

對於黑色導航欄,試試這個:

navigationController?.navigationBar.barStyle = .black
self.navigationController.navigationBar.barTintColor = [UIColor blackColor];

為 SwiftUI 解決這個問題:

            init() {
        //         For .navigationBarTitle(Text("Update"), displayMode: .automatic)
        
        let coloredAppearance = UINavigationBarAppearance()
        coloredAppearance.backgroundColor = .black
        coloredAppearance.largeTitleTextAttributes = [.foregroundColor: UIColor.white]
        
        // to make everything work normally
        UINavigationBar.appearance().standardAppearance = coloredAppearance
        UINavigationBar.appearance().scrollEdgeAppearance = coloredAppearance
        
        //         For .navigationBarTitle(Text("Update"), displayMode: .inline)
        
        //        UINavigationBar.appearance().barTintColor = .black
        //        UINavigationBar.appearance().titleTextAttributes = [.foregroundColor: UIColor.white]
    }
    
    var body: some View {
        NavigationView {
            //                     Text("Screen")
            let HomeImage = UIImage(named: "home")!
            let Home : UIBarButtonItem = UIBarButtonItem(image: HomeImage,  style: .Plain, target: self, action: "home:")
            navigationItem.rightBarButtonItem = Home
                .navigationBarTitle(Text("Update"), displayMode: .automatic)
        }
        // that means only show one view at a time no matter what device I'm working
        .navigationViewStyle(StackNavigationViewStyle())
    }

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM