由于本文中概述的原因,我不得不自定义导航栏,但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)