导航栏背景颜色
NavigationBar Background Color
我正在尝试更改将推送到导航堆栈中的导航栏背景颜色。我在 Tabbar 控制器下使用导航控制器。
当我在更改导航栏颜色后按下视图控制器时,第一次尝试它不起作用。当我通过点击标签栏项目重新加载此视图时,它起作用了。
为什么第一次尝试不起作用?
从另一个 Viewc 控制器调用的视图控制器
func showProjectDetails(indexPath: IndexPath) {
let vc = storyboard?.instantiateViewController(withIdentifier: "MyTaskVC") as! MyTaskVC
vc.viewMode = .ProjectDetails
vc.currentProjectName = projects[indexPath.row].projectName
navigationController?.pushViewController(vc, animated: true)
}
查看推送的控制器
override func viewWillAppear(_ animated: Bool) {
super.viewWillAppear(animated)
let appearance = UINavigationBarAppearance()
appearance.backgroundColor = .green
appearance.titleTextAttributes = [.foregroundColor: UIColor.white]
appearance.largeTitleTextAttributes = [.foregroundColor: UIColor.white]
UINavigationBar.appearance().tintColor = .white
UINavigationBar.appearance().standardAppearance = appearance
UINavigationBar.appearance().compactAppearance = appearance
UINavigationBar.appearance().scrollEdgeAppearance = appearance
}
将此代码添加到您的 viewDidLoad()
override func viewDidLoad() {
super.viewDidLoad()
if let navigationBar = navigationController?.navigationBar {
let appearance = UINavigationBarAppearance()
appearance.backgroundColor = .green
appearance.titleTextAttributes = [.foregroundColor: UIColor.red]
appearance.largeTitleTextAttributes = [.foregroundColor: UIColor.white]
let barAppearence = UIBarButtonItemAppearance()
barAppearence.normal.titleTextAttributes = [.foregroundColor: UIColor.yellow]
appearance.buttonAppearance = barAppearence
navigationBar.scrollEdgeAppearance = appearance
navigationBar.compactAppearance = appearance
navigationBar.standardAppearance = appearance
// Do any additional setup after loading the view.
}
}
您应该创建 UINavigationController 的子class 并对其进行自定义,如果您使用的是 Interface Builder,则可以在 Identify Inspector 中设置 NavigationController 自定义 class。
import UIKit
class YourNavigationController: UINavigationController {
override func viewWillAppear(_ animated: Bool) {
super.viewWillAppear(animated)
let barAppearance = UINavigationBar.appearance(whenContainedInInstancesOf: [YourNavigationController.self])
barAppearance.tintColor = UIColor(named: "Blue"
}
override func viewDidLoad() {
super.viewDidLoad()
}
}
你可以阅读更多here
我正在尝试更改将推送到导航堆栈中的导航栏背景颜色。我在 Tabbar 控制器下使用导航控制器。 当我在更改导航栏颜色后按下视图控制器时,第一次尝试它不起作用。当我通过点击标签栏项目重新加载此视图时,它起作用了。
为什么第一次尝试不起作用?
从另一个 Viewc 控制器调用的视图控制器
func showProjectDetails(indexPath: IndexPath) {
let vc = storyboard?.instantiateViewController(withIdentifier: "MyTaskVC") as! MyTaskVC
vc.viewMode = .ProjectDetails
vc.currentProjectName = projects[indexPath.row].projectName
navigationController?.pushViewController(vc, animated: true)
}
查看推送的控制器
override func viewWillAppear(_ animated: Bool) {
super.viewWillAppear(animated)
let appearance = UINavigationBarAppearance()
appearance.backgroundColor = .green
appearance.titleTextAttributes = [.foregroundColor: UIColor.white]
appearance.largeTitleTextAttributes = [.foregroundColor: UIColor.white]
UINavigationBar.appearance().tintColor = .white
UINavigationBar.appearance().standardAppearance = appearance
UINavigationBar.appearance().compactAppearance = appearance
UINavigationBar.appearance().scrollEdgeAppearance = appearance
}
将此代码添加到您的 viewDidLoad()
override func viewDidLoad() {
super.viewDidLoad()
if let navigationBar = navigationController?.navigationBar {
let appearance = UINavigationBarAppearance()
appearance.backgroundColor = .green
appearance.titleTextAttributes = [.foregroundColor: UIColor.red]
appearance.largeTitleTextAttributes = [.foregroundColor: UIColor.white]
let barAppearence = UIBarButtonItemAppearance()
barAppearence.normal.titleTextAttributes = [.foregroundColor: UIColor.yellow]
appearance.buttonAppearance = barAppearence
navigationBar.scrollEdgeAppearance = appearance
navigationBar.compactAppearance = appearance
navigationBar.standardAppearance = appearance
// Do any additional setup after loading the view.
}
}
您应该创建 UINavigationController 的子class 并对其进行自定义,如果您使用的是 Interface Builder,则可以在 Identify Inspector 中设置 NavigationController 自定义 class。
import UIKit
class YourNavigationController: UINavigationController {
override func viewWillAppear(_ animated: Bool) {
super.viewWillAppear(animated)
let barAppearance = UINavigationBar.appearance(whenContainedInInstancesOf: [YourNavigationController.self])
barAppearance.tintColor = UIColor(named: "Blue"
}
override func viewDidLoad() {
super.viewDidLoad()
}
}
你可以阅读更多here