Forms 如何在SwiftUI中围绕窗体部分绘制彩色笔划线

Forms 如何在SwiftUI中围绕窗体部分绘制彩色笔划线,forms,swiftui,sections,stroke,Forms,Swiftui,Sections,Stroke,使用Swift-5.4、iOS14.5、XCode12.5、 我试图在SwiftUI中的表单部分周围创建一条彩色笔划线 我的第一次试验是使用.background,如下所示: (但这不起作用,因为它包围了节项的每一行,而不是整个表单节) 我的第二次试验是使用.overlay进行的,如下所示: (但同样的错误——这不起作用) 我怎样才能在表格部分周围画一条黄线 出于完整性原因,以下是我的完整表格: struct MyView: View { var body:

使用Swift-5.4、iOS14.5、XCode12.5、

我试图在SwiftUI中的表单部分周围创建一条彩色笔划线

我的第一次试验是使用
.background
,如下所示:

(但这不起作用,因为它包围了节项的每一行,而不是整个表单节)

我的第二次试验是使用
.overlay
进行的,如下所示:

(但同样的错误——这不起作用)

我怎样才能在表格部分周围画一条黄线

出于完整性原因,以下是我的完整表格:

struct MyView: View {        
    
    var body: some View {
        
        Form {
            Section(header: Text("Section 1")) {
                HStack {
                    Text("Title 1.1")
                        .foregroundColor(Color(.label))
                    Spacer()
                    Text("Text 1.1")
                        .lineLimit(1)
                        .foregroundColor(Color(.label))
                }
                HStack {
                    Text("Title 1.2")
                        .foregroundColor(Color(.label))
                    Spacer()
                    Text("Text 1.2")
                        .lineLimit(1)
                        .foregroundColor(Color(.label))
                }
                HStack {
                    Text("Title 1.3")
                        .foregroundColor(Color(.label))
                    Spacer()
                    Text("Text 1.3")
                        .lineLimit(1)
                        .foregroundColor(Color(.label))
                }
            }
            .background(
                RoundedRectangle(cornerRadius: 16)
                .stroke(Color.yellow, lineWidth: 2)
            )

            Section(header: Text("Section 2")) {
                HStack {
                    Text("Title 2.1")
                        .foregroundColor(Color(.label))
                    Spacer()
                    Text("Text 2.1")
                        .lineLimit(1)
                        .foregroundColor(Color(.label))
                }
                HStack {
                    Text("Title 2.2")
                        .foregroundColor(Color(.label))
                    Spacer()
                    Text("Text 2.2")
                        .lineLimit(1)
                        .foregroundColor(Color(.label))
                }
                HStack {
                    Text("Title 2.3")
                        .foregroundColor(Color(.label))
                    Spacer()
                    Text("Text 2.3")
                        .lineLimit(1)
                        .foregroundColor(Color(.label))
                }
            }
            .overlay(
                RoundedRectangle(cornerRadius: 16)
                .stroke(Color.yellow, lineWidth: 2)
            )
        }
    }
}

您可以尝试以下方法:

struct MyView: View {
    var body: some View {
        Form {
            Section(header: Text("Section 1")) {
                VStack {
                    HStack {
                        Text("Title 1.1")
                            .foregroundColor(Color(.label))
                        Spacer()
                        Text("Text 1.1")
                            .lineLimit(1)
                            .foregroundColor(Color(.label))
                    }
                    HStack {
                        Text("Title 1.2")
                            .foregroundColor(Color(.label))
                        Spacer()
                        Text("Text 1.2")
                            .lineLimit(1)
                            .foregroundColor(Color(.label))
                    }
                    HStack {
                        Text("Title 1.3")
                            .foregroundColor(Color(.label))
                        Spacer()
                        Text("Text 1.3")
                            .lineLimit(1)
                            .foregroundColor(Color(.label))
                    }
                }.padding(8).overlay(RoundedRectangle(cornerRadius: 16).stroke(Color.yellow, lineWidth: 2))
            }
            
            Section(header: Text("Section 2")) {
                VStack {
                    HStack {
                        Text("Title 2.1")
                            .foregroundColor(Color(.label))
                        Spacer()
                        Text("Text 2.1")
                            .lineLimit(1)
                            .foregroundColor(Color(.label))
                    }
                    HStack {
                        Text("Title 2.2")
                            .foregroundColor(Color(.label))
                        Spacer()
                        Text("Text 2.2")
                            .lineLimit(1)
                            .foregroundColor(Color(.label))
                    }
                    HStack {
                        Text("Title 2.3")
                            .foregroundColor(Color(.label))
                        Spacer()
                        Text("Text 2.3")
                            .lineLimit(1)
                            .foregroundColor(Color(.label))
                    }
                }.padding(8).overlay(RoundedRectangle(cornerRadius: 16).stroke(Color.yellow, lineWidth: 2))
            }
        }
    }
}

谢谢你,这有点帮助。但是,您的解决方案有三个主要问题:a)黄线不在表单部分的最外边缘。b)模板截面高度比以前小得多。和c)项目之间的线条消失了。。。。我觉得用你的方法,我必须重建整个表单堆栈,这不应该是这样的,是吗?
struct MyView: View {        
    
    var body: some View {
        
        Form {
            Section(header: Text("Section 1")) {
                HStack {
                    Text("Title 1.1")
                        .foregroundColor(Color(.label))
                    Spacer()
                    Text("Text 1.1")
                        .lineLimit(1)
                        .foregroundColor(Color(.label))
                }
                HStack {
                    Text("Title 1.2")
                        .foregroundColor(Color(.label))
                    Spacer()
                    Text("Text 1.2")
                        .lineLimit(1)
                        .foregroundColor(Color(.label))
                }
                HStack {
                    Text("Title 1.3")
                        .foregroundColor(Color(.label))
                    Spacer()
                    Text("Text 1.3")
                        .lineLimit(1)
                        .foregroundColor(Color(.label))
                }
            }
            .background(
                RoundedRectangle(cornerRadius: 16)
                .stroke(Color.yellow, lineWidth: 2)
            )

            Section(header: Text("Section 2")) {
                HStack {
                    Text("Title 2.1")
                        .foregroundColor(Color(.label))
                    Spacer()
                    Text("Text 2.1")
                        .lineLimit(1)
                        .foregroundColor(Color(.label))
                }
                HStack {
                    Text("Title 2.2")
                        .foregroundColor(Color(.label))
                    Spacer()
                    Text("Text 2.2")
                        .lineLimit(1)
                        .foregroundColor(Color(.label))
                }
                HStack {
                    Text("Title 2.3")
                        .foregroundColor(Color(.label))
                    Spacer()
                    Text("Text 2.3")
                        .lineLimit(1)
                        .foregroundColor(Color(.label))
                }
            }
            .overlay(
                RoundedRectangle(cornerRadius: 16)
                .stroke(Color.yellow, lineWidth: 2)
            )
        }
    }
}
struct MyView: View {
    var body: some View {
        Form {
            Section(header: Text("Section 1")) {
                VStack {
                    HStack {
                        Text("Title 1.1")
                            .foregroundColor(Color(.label))
                        Spacer()
                        Text("Text 1.1")
                            .lineLimit(1)
                            .foregroundColor(Color(.label))
                    }
                    HStack {
                        Text("Title 1.2")
                            .foregroundColor(Color(.label))
                        Spacer()
                        Text("Text 1.2")
                            .lineLimit(1)
                            .foregroundColor(Color(.label))
                    }
                    HStack {
                        Text("Title 1.3")
                            .foregroundColor(Color(.label))
                        Spacer()
                        Text("Text 1.3")
                            .lineLimit(1)
                            .foregroundColor(Color(.label))
                    }
                }.padding(8).overlay(RoundedRectangle(cornerRadius: 16).stroke(Color.yellow, lineWidth: 2))
            }
            
            Section(header: Text("Section 2")) {
                VStack {
                    HStack {
                        Text("Title 2.1")
                            .foregroundColor(Color(.label))
                        Spacer()
                        Text("Text 2.1")
                            .lineLimit(1)
                            .foregroundColor(Color(.label))
                    }
                    HStack {
                        Text("Title 2.2")
                            .foregroundColor(Color(.label))
                        Spacer()
                        Text("Text 2.2")
                            .lineLimit(1)
                            .foregroundColor(Color(.label))
                    }
                    HStack {
                        Text("Title 2.3")
                            .foregroundColor(Color(.label))
                        Spacer()
                        Text("Text 2.3")
                            .lineLimit(1)
                            .foregroundColor(Color(.label))
                    }
                }.padding(8).overlay(RoundedRectangle(cornerRadius: 16).stroke(Color.yellow, lineWidth: 2))
            }
        }
    }
}