Swift - iOS:更改导航栏的颜色 [英] Swift - iOS : Change the color of a Navigation Bar

查看:427
本文介绍了Swift - iOS:更改导航栏的颜色的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I'm trying to change the color of my navigator bar but I found that it's only impossible if the navigator is the root one.

我正在尝试这个:

self.navigationController?.navigationBar.translucent = true

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

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

All my Viewcontrollers are related to navigator controllers. However nothing is changed. In fact I tried to make the same things from storyboard but it works only if I'm in the first navigator.

我试着阅读与此问题相关的所有内容但发现没有

I tried to read everything related to this problem but found nothing

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

I could add any item to the navigator bar like this

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


推荐答案

事实上,我发现解决方案是在 AppDelegate中使用。 siwft

In fact, i found that the solution was to use in the 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
}

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

and then in each view controller, that we need another background color or something else


  1. segue应该与show不同

  1. the segue should be different than "show"

使用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 - iOS:更改导航栏的颜色的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆