问题描述
我希望能够单击GroupBox,并将其导航到另一个视图。我尝试实现以下代码,但出现错误:
- 无法将类型“ NavigationLink ”的值转换为预期的参数类型“ GroupBoxStyleConfiguration”
- 类型'()->()'不符合'StringProtocol';只有struct / enum / class类型可以符合协议
我想知道是否可以使用GroupBox来更改视图,如果可以,怎么办?
import SwiftUI
struct ContentView: View {
// MARK - Properties
@Environment(\.presentationMode) var presentationMode
var body: some View {
NavigationView {
ScrollView(.vertical,showsIndicators: false) {
vstack(spacing: 10) {
// MARK: - SECTION 1 : Daily Catch-up
GroupBox(
// The errors start below
NavigationLink({
WellnessLabelView(labelText: "Daily Catch-up",labelImage: "chevron.right")
) {
Divider().padding(.vertical,4)
HStack(alignment: .center,spacing: 10) {
Text("This app was developed with the idea that expectant families deserve easy access to resources.")
.font(.footnote)}
}
解决方法
您必须制作自己的GroupBoxStyle
https://itnext.io/swiftui-groupbox-for-ios-bb16aa71469c
struct HealthGroupBoxStyle<V: View>: GroupBoxStyle {
var color: Color
var destination: V
var date: Date?
@ScaledMetric var size: CGFloat = 1
func makeBody(configuration: Configuration) -> some View {
NavigationLink(destination: destination) {
GroupBox(label: HStack {
configuration.label.foregroundColor(color)
Spacer()
if date != nil {
Text("\(date!)").font(.footnote).foregroundColor(.secondary).padding(.trailing,4)
}
Image(systemName: "chevron.right").foregroundColor(Color(.systemGray4)).imageScale(.small)
}) {
configuration.content.padding(.top)
}
}.buttonStyle(PlainButtonStyle())
}
}