将返回按钮添加到 UINavigationBar SwiftUI
Add Back Button to UINavigationBar SwiftUI
出于 this post 中概述的原因,我必须自定义导航栏,但 SwiftUI 现在不会自动为我生成后退按钮。如何添加它们?
查看我的示例代码:
struct ContentView: View {
init() {
let coloredAppearance = UINavigationBarAppearance()
coloredAppearance.configureWithOpaqueBackground()
coloredAppearance.backgroundColor = .white
coloredAppearance.titleTextAttributes = [.foregroundColor: UIColor.black]
coloredAppearance.largeTitleTextAttributes = [.foregroundColor: UIColor.black]
UINavigationBar.appearance().standardAppearance = coloredAppearance
UINavigationBar.appearance().compactAppearance = coloredAppearance
UINavigationBar.appearance().scrollEdgeAppearance = coloredAppearance
UINavigationBar.appearance().tintColor = .white
}
var body: some View {
NavigationView {
NavigationLink(destination: NavigationLink(destination: Color.purple) {
Text("Continue")
.navigationTitle("Nav Title 2")
.navigationBarTitleDisplayMode(.inline)
}) {
Text("Continue")
.navigationTitle("Nav Title 1")
.navigationBarTitleDisplayMode(.inline)
}
}
}
}
在您的示例代码中,生成了后退按钮,但颜色为白色,因此它们在导航栏中不可见。
要显示后退按钮更改:
UINavigationBar.appearance().tintColor = .white
收件人:
UINavigationBar.appearance().tintColor = UIColor(.accentColor)
出于 this post 中概述的原因,我必须自定义导航栏,但 SwiftUI 现在不会自动为我生成后退按钮。如何添加它们?
查看我的示例代码:
struct ContentView: View {
init() {
let coloredAppearance = UINavigationBarAppearance()
coloredAppearance.configureWithOpaqueBackground()
coloredAppearance.backgroundColor = .white
coloredAppearance.titleTextAttributes = [.foregroundColor: UIColor.black]
coloredAppearance.largeTitleTextAttributes = [.foregroundColor: UIColor.black]
UINavigationBar.appearance().standardAppearance = coloredAppearance
UINavigationBar.appearance().compactAppearance = coloredAppearance
UINavigationBar.appearance().scrollEdgeAppearance = coloredAppearance
UINavigationBar.appearance().tintColor = .white
}
var body: some View {
NavigationView {
NavigationLink(destination: NavigationLink(destination: Color.purple) {
Text("Continue")
.navigationTitle("Nav Title 2")
.navigationBarTitleDisplayMode(.inline)
}) {
Text("Continue")
.navigationTitle("Nav Title 1")
.navigationBarTitleDisplayMode(.inline)
}
}
}
}
在您的示例代码中,生成了后退按钮,但颜色为白色,因此它们在导航栏中不可见。
要显示后退按钮更改:
UINavigationBar.appearance().tintColor = .white
收件人:
UINavigationBar.appearance().tintColor = UIColor(.accentColor)