2024-11-27
首先要embed in Navigate Controller:
选中第一个界面 - Editor - embed in - Navigate Controller
添加元素bar button
改成名字-Logout
在viewDidLoad里面增加:
navigationItem.hidesBackButton = true
在viewDidLoad里面增加:
title = "⚡️FlashChat"
在viewWillAppear
里面设置。
为什么不设置在viewDidLoad
?因为它会快于导航条的渲染。而viewWillAppear出现在导航条渲染后。
var defaultAppearance: UINavigationBarAppearance?
override func viewWillAppear(_ animated: Bool) {
guard let navBar = navigationController?.navigationBar else {fatalError("Navigation controller does not exist")}
// 保存默认外观配置,以便后续恢复
defaultAppearance = navBar.standardAppearance.copy()
let appearance = UINavigationBarAppearance()
appearance.backgroundColor = .red
navBar.standardAppearance = appearance
navBar.scrollEdgeAppearance = appearance
}
在viewWillDisappear
里面设置。
override func viewWillDisappear(_ animated: Bool) {
super.viewWillDisappear(animated)
// 恢复默认外观
if let defaultAppearance = defaultAppearance,
let navBar = navigationController?.navigationBar {
navBar.standardAppearance = defaultAppearance
navBar.scrollEdgeAppearance = defaultAppearance
}
}
.tintColor
appearance.titleTextAttributes
代码示例:
guard let menuColor = UIColor(hexString: colorHex) else {
fatalError("Error color")}
// 保存默认外观配置,以便后续恢复
defaultAppearance = navBar.standardAppearance.copy()
let appearance = UINavigationBarAppearance()
appearance.backgroundColor = UIColor(hexString: colorHex)
//改变back按钮颜色
navBar.tintColor = UIColor(contrastingBlackOrWhiteColorOn: menuColor , isFlat: true)
//改变标题颜色
appearance.titleTextAttributes = [.foregroundColor: UIColor(contrastingBlackOrWhiteColorOn: menuColor, isFlat: true)]
// 设置 Large Title 的颜色
appearance.largeTitleTextAttributes = [.foregroundColor: UIColor(contrastingBlackOrWhiteColorOn: menuColor, isFlat: true)]
navBar.standardAppearance = appearance
navBar.scrollEdgeAppearance = appearance
searchBar.barTintColor = UIColor(hexString: colorHex)
在AppDelegate的第一个func里面输入:
//全局样式-顶部菜单颜色
let appearance = UINavigationBarAppearance()
appearance.backgroundColor = UIColor(named: K.BrandColors.blue) // 设置背景色
// 设置标题字体和颜色
appearance.titleTextAttributes = [
.font: UIFont.systemFont(ofSize: 25, weight: .black), // 设置字号为28
.foregroundColor: UIColor.white // 设置颜色为白色
]
UINavigationBar.appearance().standardAppearance = appearance
UINavigationBar.appearance().scrollEdgeAppearance = appearance
UINavigationBar.appearance().compactAppearance = appearance