我正在使用一个选择器视图,允许用户为整个应用程序选择颜色主题。

我计划改变导航栏的颜色,背景和可能的标签栏(如果可能的话)。

我一直在研究如何做到这一点,但找不到任何Swift的例子。谁能给我一个代码的例子,我需要用来改变导航栏的颜色和导航栏的文本颜色?

选取器视图已经设置好,我正在寻找更改UI颜色的代码。


当前回答

更新为Swift 3, 4, 4.2, 5+

// 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

Swift 4.2, 5+

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

如果你想使用大标题,添加这一行:

UINavigationBar.navigationBar.prefersLargeTitles = true

也可以查看这里:https://github.com/hasnine/iOSUtilitiesSource

其他回答

我写这篇文章是为了那些仍然对这里的解决方案有问题的人。

我使用的是Xcode版本11.4 (11E146)。为我工作的是:

navigationController?.navigationBar.barTintColor = UIColor.white
navigationController?.navigationBar.tintColor = UIColor.black

但是!如果你将storyboard中的barTintColor设置为“default”以外的任何值,这两行代码将不起作用。

小心,在Storyboard中设置回默认的barTintColor。 哦,苹果……

斯威夫特3

UINavigationBar.appearance().barTintColor = UIColor(colorLiteralRed: 51/255, green: 90/255, blue: 149/255, alpha: 1)

这将设置你的导航栏颜色像Facebook栏颜色:)

没有一个解决方案对我有效,所以我分享一个有效的。

Swift 5, Xcode 13.4.1

将以下内容放入viewDidLoad():

let appearance = UINavigationBarAppearance()
        appearance.configureWithOpaqueBackground()
        appearance.backgroundColor = UIColor.systemBlue
        appearance.titleTextAttributes = [.foregroundColor: UIColor.white]
        navigationItem.standardAppearance = appearance
        navigationItem.scrollEdgeAppearance = appearance
        navigationItem.compactAppearance = appearance

这就是结果

记住将检查器中的所有设置设置为默认值。如果你需要更多的调整,搜索开发者文档“自定义你的应用程序的导航栏”

希望这能有所帮助。

UINavigationBar.appearance().barTintColor = UIColor(red: 46.0/255.0, green: 14.0/255.0, blue: 74.0/255.0, alpha: 1.0)
UINavigationBar.appearance().tintColor = UIColor.whiteColor()
UINavigationBar.appearance().titleTextAttributes = [NSForegroundColorAttributeName : UIColor.whiteColor()]

只需将这一行粘贴到代码中的didFinishLaunchingWithOptions中。

这里有一些非常基本的外观定制,你可以应用于整个应用程序:

UINavigationBar.appearance().backgroundColor = UIColor.greenColor()
UIBarButtonItem.appearance().tintColor = UIColor.magentaColor()
//Since iOS 7.0 UITextAttributeTextColor was replaced by NSForegroundColorAttributeName
UINavigationBar.appearance().titleTextAttributes = [UITextAttributeTextColor: UIColor.blueColor()]
UITabBar.appearance().backgroundColor = UIColor.yellowColor();

斯威夫特5.4.2:

UINavigationBar.appearance().backgroundColor = .green // backgorund color with gradient
// or
UINavigationBar.appearance().barTintColor = .green  // solid color
    
UIBarButtonItem.appearance().tintColor = .magenta
UINavigationBar.appearance().titleTextAttributes = [NSAttributedString.Key.foregroundColor : UIColor.blue]
UITabBar.appearance().barTintColor = .yellow

更多关于UIAppearance API在Swift你可以阅读这里。