如何在SwiftUI中创建一个视图内部嵌套另一个视图?

6

我需要做一件简单的事情,但不知道如何实现。

所以我需要创建一个视图,这个视图已经存在于另一个视图中。下面是它的样子↓

这是我的按钮:

struct CircleButton: View {
    var body: some View {
        
        Button(action: {
            self
            
        }, label: {
            Text("+")
                .font(.system(size: 42))
                .frame(width: 57, height: 50)
                .foregroundColor(Color.white)
                .padding(.bottom, 7)
        })
        .background(Color(#colorLiteral(red: 0.4509803922, green: 0.8, blue: 0.5490196078, alpha: 1)))
        .cornerRadius(50)
        .padding()
        .shadow(color: Color.black.opacity(0.15),
                radius: 3,
                x: 0,
                y: 4)
    }
}

当我点击按钮时,下面是我想要放置的视图↓

struct IssueCardView: View {
    
    var body: some View {
        
        ZStack (alignment: .leading) {
            Rectangle()
                
                .fill(Color.white)
                .frame(height: 50)
                .shadow(color: .black, radius: 20, x: 0, y: 4)
                .cornerRadius(8)
            
            VStack (alignment: .leading) {
                
                    Rectangle()
                        .fill(Color(#colorLiteral(red: 0.6550863981, green: 0.8339114785, blue: 0.7129291892, alpha: 1)))
                        .frame(width: 30, height: 8)
                        .cornerRadius(8)
                        .padding(.horizontal, 10)
                
                    
                    Text("Some text on card here")
                        .foregroundColor(Color(UIColor.dark.main))
                        .font(.system(size: 14))
                        .fontWeight(.regular)
                        .padding(.horizontal, 10)
            }

        }
    }
}

这里有一个视图,我想把这个 IssueCardView 放在下面 ↓。现在是手动完成的,但我想通过按钮来生成此视图。
struct TaskListView: View {
    
    var body: some View {
        ScrollView(.vertical, showsIndicators: false, content: {
            VStack (alignment: .leading, spacing: 8) {
                
                **IssueCardView()
                IssueCardView()
                IssueCardView()
                IssueCardView()
                IssueCardView()**
                
            }
            .frame(minWidth: 320, maxWidth: 500, minHeight: 500, maxHeight: .infinity, alignment: .topLeading)
            .padding(.horizontal, 0)
        })
    }
}
1个回答

2

虽然它可以与scrollViewstack一起使用,但对于这些类型的UI问题,您应该使用一个List(正如您在TaskListView名称中已经提到的那样)。

struct TaskListView: View {

    typealias Issue = String // This is temporary, because I didn't have the original `Issue` type

    @State var issues: [Issue] = ["Some issue", "Some issue"]

    var body: some View {
        ZStack {
            List(issues, id: \.self) { issue in
                IssueCardView()
            }

            CircleButton {
                self.issues += ["new issue"]
            }
        }
    }
}

我已经在CircleButton中添加了let action: ()->(),这样我就可以将操作传递给它。


谢谢。这很有效。我还有一个问题,我的按钮在单独的视图中,因为将来它会有在不同的任务列表视图中添加此 IssueCardView 的能力。也许你可以帮我想一想如何使这个按钮更自主? - Nick Samoylov
请随时提出新问题(https://stackoverflow.com/questions/ask)。您还可以在此帖子下方评论新问题的链接,以便我通知并帮助您;) - Mojtaba Hosseini
抱歉,你在哪里添加了“let action: ()->()”? - Nick Samoylov
恰好在自定义按钮视图的第一行 - Mojtaba Hosseini
关于你的另一个问题,你应该为你的应用程序建立一个模型。然后你可以将更改发布给所有订阅者。 - Mojtaba Hosseini

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接