我正在使用一个选择器视图,允许用户为整个应用程序选择颜色主题。
我计划改变导航栏的颜色,背景和可能的标签栏(如果可能的话)。
我一直在研究如何做到这一点,但找不到任何Swift的例子。谁能给我一个代码的例子,我需要用来改变导航栏的颜色和导航栏的文本颜色?
选取器视图已经设置好,我正在寻找更改UI颜色的代码。
我正在使用一个选择器视图,允许用户为整个应用程序选择颜色主题。
我计划改变导航栏的颜色,背景和可能的标签栏(如果可能的话)。
我一直在研究如何做到这一点,但找不到任何Swift的例子。谁能给我一个代码的例子,我需要用来改变导航栏的颜色和导航栏的文本颜色?
选取器视图已经设置好,我正在寻找更改UI颜色的代码。
当前回答
斯威夫特3
UINavigationBar.appearance().barTintColor = UIColor(colorLiteralRed: 51/255, green: 90/255, blue: 149/255, alpha: 1)
这将设置你的导航栏颜色像Facebook栏颜色:)
其他回答
在Swift 4中
您可以更改导航栏的颜色。只需在viewDidLoad()中使用下面的代码片段
导航栏颜色
self.navigationController?.navigationBar.barTintColor = UIColor.white
导航栏文字颜色
self.navigationController?.navigationBar.titleTextAttributes = [NSAttributedStringKey.foregroundColor: UIColor.purple]
对于iOS 11大标题导航栏,你需要使用largeTitleTextAttributes属性
self.navigationController?.navigationBar.largeTitleTextAttributes = [NSAttributedStringKey.foregroundColor: UIColor.purple]
Swift 5,一个简单的UINavigationController扩展方法。在这个答案的底部是扩展和预览。
第一个视图控制器(Home):
override func viewWillAppear(_ animated: Bool) {
super.viewWillAppear(animated)
navigationController?.setTintColor(.white)
navigationController?.backgroundColor(.orange)
}
第二个视图控制器(详细信息):
override func viewWillAppear(_ animated: Bool) {
super.viewWillAppear(animated)
navigationController?.transparentNavigationBar()
navigationController?.setTintColor(.black)
}
UINavigationController的扩展:
extension UINavigationController {
func transparentNavigationBar() {
self.navigationBar.setBackgroundImage(UIImage(), for: .default)
self.navigationBar.shadowImage = UIImage()
self.navigationBar.isTranslucent = true
}
func setTintColor(_ color: UIColor) {
self.navigationBar.titleTextAttributes = [NSAttributedString.Key.foregroundColor: color]
self.navigationBar.tintColor = color
}
func backgroundColor(_ color: UIColor) {
navigationBar.setBackgroundImage(nil, for: .default)
navigationBar.barTintColor = color
navigationBar.shadowImage = UIImage()
}
}
故事板视图:
预览:
更新为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
iOs 14 +
init() {
let appearance = UINavigationBarAppearance()
appearance.shadowColor = .clear // gets also rid of the bottom border of the navigation bar
appearance.configureWithTransparentBackground()
UINavigationBar.appearance().standardAppearance = appearance
UINavigationBar.appearance().scrollEdgeAppearance = appearance
}
以下代码适用于iOS 15
if #available(iOS 15, *) {
// Navigation Bar background color
let appearance = UINavigationBarAppearance()
appearance.configureWithOpaqueBackground()
appearance.backgroundColor = UIColor.yourColor
// setup title font color
let titleAttribute = [NSAttributedString.Key.font: UIFont.systemFont(ofSize: 25, weight: .bold), NSAttributedString.Key.foregroundColor: UIColor.yourColor]
appearance.titleTextAttributes = titleAttribute
navigationController?.navigationBar.standardAppearance = appearance
navigationController?.navigationBar.scrollEdgeAppearance = appearance
}