我正在使用一个选择器视图来允许用户选择整个应用程序的颜色主题。
我计划更改导航栏、背景和可能的选项卡栏(如果可能)的颜色。
我一直在研究如何做到这一点,但找不到任何Swift示例。请问是否有人可以给我提供代码示例,以便我可以更改导航栏颜色和导航栏文本颜色?
选择器视图已设置好,我只是在寻找更改UI颜色的代码。
我正在使用一个选择器视图来允许用户选择整个应用程序的颜色主题。
我计划更改导航栏、背景和可能的选项卡栏(如果可能)的颜色。
我一直在研究如何做到这一点,但找不到任何Swift示例。请问是否有人可以给我提供代码示例,以便我可以更改导航栏颜色和导航栏文本颜色?
选择器视图已设置好,我只是在寻找更改UI颜色的代码。
navigationController?.navigationBar.barTintColor = UIColor.green
用您想要的任何UIColor替换greenColor,如果您喜欢,也可以使用RGB。
导航栏文本:
navigationController?.navigationBar.titleTextAttributes = [.foregroundColor: UIColor.orange]
用你喜欢的颜色替换orangeColor。
选项卡栏:
tabBarController?.tabBar.barTintColor = UIColor.brown
选项卡文本:
tabBarController?.tabBar.tintColor = UIColor.yellow
在最后两个中,用您选择的颜色替换brownColor和yellowColor。
以下是可以在整个应用程序中应用的一些非常基本的外观自定义:
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();
Swift 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
关于在Swift中使用UIAppearance
API的更多信息,请阅读这里。
更新至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
Swift 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
UINavigationBar.appearance().prefersLargeTitles = true
后,这段代码不起作用了,请问如何解决? - Zhou HaiboUINavigationBar.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
方法中。
在 AppDelegate 中,这已全局更改了 NavBar 的格式并移除了底部线条/边框(这是大多数人的问题区域),以便为您和其他人提供我认为您正在寻找的内容:
func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool {
UINavigationBar.appearance().setBackgroundImage(UIImage(), forBarPosition: UIBarPosition.Any, barMetrics: UIBarMetrics.Default)
UINavigationBar.appearance().shadowImage = UIImage()
UINavigationBar.appearance().tintColor = UIColor.whiteColor()
UINavigationBar.appearance().barTintColor = Style.SELECTED_COLOR
UINavigationBar.appearance().translucent = false
UINavigationBar.appearance().clipsToBounds = false
UINavigationBar.appearance().backgroundColor = Style.SELECTED_COLOR
UINavigationBar.appearance().titleTextAttributes = [NSFontAttributeName : (UIFont(name: "FONT NAME", size: 18))!, NSForegroundColorAttributeName: UIColor.whiteColor()] }
那么您可以设置一个 Constants.swift 文件,其中包含一个Style结构体,其中包括颜色、字体等。您可以将tableView/pickerView添加到任何ViewController中,并使用"availableThemes"数组允许用户更改themeColor。
这个美妙的地方在于,您可以在整个应用程序中使用一个引用来表示每个颜色,它将基于用户选择的"主题"进行更新,如果没有默认为theme1():
import Foundation
import UIKit
struct Style {
static let availableThemes = ["Theme 1","Theme 2","Theme 3"]
static func loadTheme(){
let defaults = NSUserDefaults.standardUserDefaults()
if let name = defaults.stringForKey("Theme"){
// Select the Theme
if name == availableThemes[0] { theme1() }
if name == availableThemes[1] { theme2() }
if name == availableThemes[2] { theme3() }
}else{
defaults.setObject(availableThemes[0], forKey: "Theme")
theme1()
}
}
// Colors specific to theme - can include multiple colours here for each one
static func theme1(){
static var SELECTED_COLOR = UIColor(red:70/255, green: 38/255, blue: 92/255, alpha: 1) }
static func theme2(){
static var SELECTED_COLOR = UIColor(red:255/255, green: 255/255, blue: 255/255, alpha: 1) }
static func theme3(){
static var SELECTED_COLOR = UIColor(red:90/255, green: 50/255, blue: 120/255, alpha: 1) } ...
在Storyboard上完成此操作(使用Interface Builder Inspector)
通过IBDesignable
,我们可以为UINavigationController
添加更多选项,并在Storyboard上进行微调。首先,将以下代码添加到您的项目中。
@IBDesignable extension UINavigationController {
@IBInspectable var barTintColor: UIColor? {
set {
guard let uiColor = newValue else { return }
navigationBar.barTintColor = uiColor
}
get {
guard let color = navigationBar.barTintColor else { return nil }
return color
}
}
}
然后只需在Storyboard上设置导航控制器的属性。
这种方法也可用于从Storyboard中管理导航栏文本的颜色:
@IBInspectable var barTextColor: UIColor? {
set {
guard let uiColor = newValue else {return}
navigationBar.titleTextAttributes = [NSAttributedStringKey.foregroundColor: uiColor]
}
get {
guard let textAttributes = navigationBar.titleTextAttributes else { return nil }
return textAttributes[NSAttributedStringKey.foregroundColor] as? UIColor
}
}
Swift 4:
完美运行的代码,用于在应用程序级别更改导航栏外观。
// MARK: Navigation Bar Customisation
// To change background colour.
UINavigationBar.appearance().barTintColor = .init(red: 23.0/255, green: 197.0/255, blue: 157.0/255, alpha: 1.0)
// To change colour of tappable items.
UINavigationBar.appearance().tintColor = .white
// To apply textAttributes to title i.e. colour, font etc.
UINavigationBar.appearance().titleTextAttributes = [.foregroundColor : UIColor.white,
.font : UIFont.init(name: "AvenirNext-DemiBold", size: 22.0)!]
// To control navigation bar's translucency.
UINavigationBar.appearance().isTranslucent = false
快乐编程!
以下代码适用于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
}
UINavigationBar.appearance().barTintColor
对我很有用。
SWIFT 4 - 平滑过渡(最佳解决方案):
如果您从导航控制器返回并且必须在推出的导航控制器上设置不同的颜色,您应该使用
override func willMove(toParentViewController parent: UIViewController?) {
navigationController?.navigationBar.barTintColor = .white
navigationController?.navigationBar.tintColor = Constants.AppColor
}
将其放在viewWillAppear中会导致过渡效果不够流畅,建议使用其他方式。
SWIFT 4.2
override func willMove(toParent parent: UIViewController?) {
navigationController?.navigationBar.barTintColor = UIColor.black
navigationController?.navigationBar.tintColor = UIColor.black
}