gpt4 book ai didi

ios - View Controller 出现后 UINavigationBarAppearance 更新

转载 作者:行者123 更新时间:2023-11-29 05:24:27 24 4
gpt4 key购买 nike

使用新的 UINavigationBarAppearance 时,似乎没有(或者至少没有明显的)方法来更新 View Controller 上的导航样式 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 等。已尝试使用它来代替,它似乎可以完成工作!

关于ios - View Controller 出现后 UINavigationBarAppearance 更新,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58318955/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com