Swift - iOS:更改导航栏的颜色

Ant*_*ine 46 ios uinavigation swift

我正在尝试更改导航栏的颜色,但我发现如果导航器是根导线,那是唯一不可能的.

我正在尝试这个:

self.navigationController?.navigationBar.translucent = true

self.navigationController!.navigationBar.barTintColor = UIColor.blueColor()
Run Code Online (Sandbox Code Playgroud)

我所有的Viewcontrollers都与导航控制器有关.然而,没有任何改变.事实上,我试图从故事板中制作相同的东西,但只有当我在第一个导航器中时才有效.

我试图阅读与此问题相关的所有内容,但一无所获

我可以像这样添加任何项目到导航栏

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

Ant*_*ine 93

事实上,我发现解决方案是用于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
}
Run Code Online (Sandbox Code Playgroud)

然后在每个视图控制器中,我们需要另一种背景颜色或其他东西

  1. segue应该不同于"show"

  2. 使用功能 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()]
    }
    
    Run Code Online (Sandbox Code Playgroud)

  • @Takasur 用于 swift 4,NSAttributedStringKey.foregroundColor (2认同)

iAj*_*iAj 46

针对Swift 3进行了更新

    UINavigationBar.appearance().barTintColor = .black
    UINavigationBar.appearance().tintColor = .white
    UINavigationBar.appearance().titleTextAttributes = [NSForegroundColorAttributeName: UIColor.white]
    UINavigationBar.appearance().isTranslucent = false
Run Code Online (Sandbox Code Playgroud)

斯威夫特4

    UINavigationBar.appearance().barTintColor = .black
    UINavigationBar.appearance().tintColor = .white
    UINavigationBar.appearance().titleTextAttributes = [NSAttributedStringKey.foregroundColor: UIColor.white]
    UINavigationBar.appearance().isTranslucent = false
Run Code Online (Sandbox Code Playgroud)


Hem*_*ang 20

Swift 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]
Run Code Online (Sandbox Code Playgroud)

Swift 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]
Run Code Online (Sandbox Code Playgroud)


Yog*_* Mv 7

要在整个应用程序中更改导航栏主题颜色,可以使用UiNavigation栏外观来完成此操作.

UINavigationBar.appearance().barTintColor = UIColor.redColor()
Run Code Online (Sandbox Code Playgroud)