2016-10-08 217 views
24

我试图更改导航栏的颜色,但是我发现如果导航器是根目录的话,这是不可能的。Swift - iOS:更改导航栏的颜色

我想这一点:

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 

回答

56

事实上,我发现,解决办法是在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应该比 “秀” 输精管

  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()] 
    

    }

+0

这与尤里卡荚 –

4

对于整个完整的应用程序,你改变导航栏的主题颜色可以使用UiNavigation bar外观来做到这一点。

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

如果您的视图控制器嵌入导航控制器,那么你可以删除这个默认的导航栏,可以使用该视图控制器的自定义导航栏。

那么你可以做看起来像

UINavigationBar.appearance().barTintColor = UIColor(red: 46.0/255.0, green: 14.0/255.0, blue: 74.0/255.0, alpha: 1.0) 
0
self.navigationController.navigationBar.barTintColor = [UIColor blackColor]; 
4

斯威夫特4.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 = [NSAttributedStringKey.foregroundColor: UIColor.white] 

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] 
28

更新了夫特3

// setup navBar..... 
    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 
+0

效果很好做我必须在appdelegate.siwft文件中执行此操作? – TheSwiftGuy77

+0

FUNC应用(_应用:UIApplication的,didFinishLaunchingWithOptions launchOptions:[UIApplicationLaunchOptionsKey:任何]) - >布尔{ //设置导航栏..... } – iajmeri43

+0

@PrashannaChudal是,U可以..代码这在上述的appdelegate方法(见上面的评论).. – iajmeri43

4

进行以下更新到AppDelegate.Swift文件即 UINavigationBar.appearance() .barTintColor = UIColor(红色:x.xx,绿色:x.xx,蓝色:x.xx,alph答:1。0)

请参考下面的例子

进口的UIKit

@UIApplicationMain 类的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:. 
} 

}