视图控制器出现后 UINavigationBarAppearance 更新
UINavigationBarAppearance update after view controller has appeared
似乎没有——或者至少没有明显的——在使用新的 UINavigationBarAppearance
apis 时更新视图控制器上的导航样式的方法。虽然这些 API 远远优于旧的样式方法,特别是在需要透明导航栏时,我需要在多个应用程序中能够在透明导航和不透明导航之间切换。
有人知道解决这个问题的方法吗?或者我可能会遗漏什么?下面的示例代码!
var styles: [UINavigationBarAppearance] = {
let style1 = UINavigationBarAppearance()
style1.configureWithOpaqueBackground()
style1.backgroundColor = .white
style1.titleTextAttributes = [.foregroundColor: UIColor.black]
style1.largeTitleTextAttributes = [.foregroundColor: UIColor.black]
let buttonAppearance = UIBarButtonItemAppearance()
buttonAppearance.normal.titleTextAttributes = [
.foregroundColor: UIColor.black
]
style1.buttonAppearance = buttonAppearance
let style2 = UINavigationBarAppearance()
style2.configureWithOpaqueBackground()
style2.backgroundColor = .systemBlue
style2.titleTextAttributes = [.foregroundColor: UIColor.systemGreen]
style2.largeTitleTextAttributes = [.foregroundColor: UIColor.systemGreen]
let button2Appearance = UIBarButtonItemAppearance()
button2Appearance.normal.titleTextAttributes = [
.foregroundColor: UIColor.systemGreen
]
style2.buttonAppearance = button2Appearance
let style3 = UINavigationBarAppearance()
style3.configureWithTransparentBackground()
style3.titleTextAttributes = [.foregroundColor: UIColor.systemBlue]
style3.largeTitleTextAttributes = [.foregroundColor: UIColor.systemBlue]
let button3Appearance = UIBarButtonItemAppearance()
button3Appearance.normal.titleTextAttributes = [
.foregroundColor: UIColor.systemBlue
]
style3.buttonAppearance = button3Appearance
return [style1, style2, style3]
}()
var currentStyle: UINavigationBarAppearance?
override func viewDidLoad() {
currentStyle = styles.first
super.viewDidLoad()
// Do any additional setup after loading the view.
}
@IBAction func switchNavigavigationStyles(_ sender: Any) {
guard let currentStyle = currentStyle else {
return
}
guard let styleIndex = styles.firstIndex(of: currentStyle) else {
return
}
var nextIndex = styleIndex + 1
if nextIndex == styles.count {
nextIndex = 0
}
navigationController?.navigationBar.scrollEdgeAppearance = styles[nextIndex]
navigationController?.navigationBar.compactAppearance = styles[nextIndex]
navigationController?.navigationBar.standardAppearance = styles[nextIndex]
navigationController?.navigationBar.setNeedsDisplay() // Attempting to force it to redraw!
navigationController?.view.setNeedsDisplay()
self.currentStyle = styles[nextIndex]
}
知道了!因此,在重新观看 WWDC 视频后,似乎要在您设置的特定屏幕上进行配置 navigationItem.standardAppearance
e.t.c。已尝试改用它,它似乎可以完成工作!
似乎没有——或者至少没有明显的——在使用新的 UINavigationBarAppearance
apis 时更新视图控制器上的导航样式的方法。虽然这些 API 远远优于旧的样式方法,特别是在需要透明导航栏时,我需要在多个应用程序中能够在透明导航和不透明导航之间切换。
有人知道解决这个问题的方法吗?或者我可能会遗漏什么?下面的示例代码!
var styles: [UINavigationBarAppearance] = {
let style1 = UINavigationBarAppearance()
style1.configureWithOpaqueBackground()
style1.backgroundColor = .white
style1.titleTextAttributes = [.foregroundColor: UIColor.black]
style1.largeTitleTextAttributes = [.foregroundColor: UIColor.black]
let buttonAppearance = UIBarButtonItemAppearance()
buttonAppearance.normal.titleTextAttributes = [
.foregroundColor: UIColor.black
]
style1.buttonAppearance = buttonAppearance
let style2 = UINavigationBarAppearance()
style2.configureWithOpaqueBackground()
style2.backgroundColor = .systemBlue
style2.titleTextAttributes = [.foregroundColor: UIColor.systemGreen]
style2.largeTitleTextAttributes = [.foregroundColor: UIColor.systemGreen]
let button2Appearance = UIBarButtonItemAppearance()
button2Appearance.normal.titleTextAttributes = [
.foregroundColor: UIColor.systemGreen
]
style2.buttonAppearance = button2Appearance
let style3 = UINavigationBarAppearance()
style3.configureWithTransparentBackground()
style3.titleTextAttributes = [.foregroundColor: UIColor.systemBlue]
style3.largeTitleTextAttributes = [.foregroundColor: UIColor.systemBlue]
let button3Appearance = UIBarButtonItemAppearance()
button3Appearance.normal.titleTextAttributes = [
.foregroundColor: UIColor.systemBlue
]
style3.buttonAppearance = button3Appearance
return [style1, style2, style3]
}()
var currentStyle: UINavigationBarAppearance?
override func viewDidLoad() {
currentStyle = styles.first
super.viewDidLoad()
// Do any additional setup after loading the view.
}
@IBAction func switchNavigavigationStyles(_ sender: Any) {
guard let currentStyle = currentStyle else {
return
}
guard let styleIndex = styles.firstIndex(of: currentStyle) else {
return
}
var nextIndex = styleIndex + 1
if nextIndex == styles.count {
nextIndex = 0
}
navigationController?.navigationBar.scrollEdgeAppearance = styles[nextIndex]
navigationController?.navigationBar.compactAppearance = styles[nextIndex]
navigationController?.navigationBar.standardAppearance = styles[nextIndex]
navigationController?.navigationBar.setNeedsDisplay() // Attempting to force it to redraw!
navigationController?.view.setNeedsDisplay()
self.currentStyle = styles[nextIndex]
}
知道了!因此,在重新观看 WWDC 视频后,似乎要在您设置的特定屏幕上进行配置 navigationItem.standardAppearance
e.t.c。已尝试改用它,它似乎可以完成工作!