I'm attempting to create a function that will return a swiftUI container. Something like this:
However, I really want toe content input to be more generic.
When I change the function declaration to
The compiler seems to require me to specify the details of the content of the ZStack.
Alternatively, when I try this:
It complains that Content is an undeclared type.
any/all help greatly appreciated.
thanks,
Mike
Code Block func myStack(content: Text) -> some View { ZStack{ if self.shouldBeHorizontal() { HStack(alignment: .center, spacing: 10, content: {content}) } else { VStack(alignment: .center, spacing: 10, content: {content}) } } }
However, I really want toe content input to be more generic.
When I change the function declaration to
Code Block func myStack(content: ZStack) -> some View {
The compiler seems to require me to specify the details of the content of the ZStack.
Alternatively, when I try this:
Code Block func myStack(content: () -> Content) -> some View {
It complains that Content is an undeclared type.
any/all help greatly appreciated.
thanks,
Mike
You can make your method explicitly generic.
Please try something like this:
or this:
Please try something like this:
Code Block func myStack<Content: View>(content: Content) -> some View { ZStack{ if self.shouldBeHorizontal() { HStack(alignment: .center, spacing: 10, content: {content}) } else { VStack(alignment: .center, spacing: 10, content: {content}) } } }
or this:
Code Block func myStack<Content: View>(@ViewBuilder content: ()->Content) -> some View { ZStack{ if self.shouldBeHorizontal() { HStack(alignment: .center, spacing: 10, content: content) } else { VStack(alignment: .center, spacing: 10, content: content) } } }