Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to create View inside another view with SwiftUI?

Tags:

ios

swift

swiftui

I need to make such a simple thing, but can't figure out how.

So I need to create a View which I already have inside another view. Here how it looks like now ↓

Here's my button:

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)
    }
}

Here's a view which I want to place when I tap the button ↓

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)
            }

        }
    }
}

Here's a view where I want to place this IssueCardView ↓. Instead of doing it manually like now I want to generate this View with button.

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)
        })
    }
}
like image 283
Nick Samoylov Avatar asked Oct 20 '19 15:10

Nick Samoylov


People also ask

How do I add a view in SwiftUI?

In Xcode, custom views are contained within SwiftUI View files. When a new SwiftUI project is created, Xcode will create a single SwiftUI View file containing a single custom view consisting of a single Text view component. Additional view files can be added to the project by selecting the File -> New -> File…


1 Answers

Although it works with scrollView and stack, You should use a List for these kind of UI issues (as you already mentioned in the name of 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"]
            }
        }
    }
}

I have added let action: ()->() to CircleButton. So I can pass the action to it.

like image 182
Mojtaba Hosseini Avatar answered Nov 10 '22 23:11

Mojtaba Hosseini