简体   繁体   中英

Best method to return multiple views in SwiftUI

var myView: some View {
    Group {
        Text("Hello")
        Text("Bye")
    }
}
@ViewBuilder var myView: some View {
    Text("Hello")
    Text("Bye")
}

Should I use a Group or @ViewBuilder , are there any advantages in using one over the other in terms of performance and customizability? If not, is there a convention in place to use one rather than the other?

Personally I think that you shouldn't do both, if you need two vertically aligned Text views then just wrap them in VStack (or whatever you need) and make a component for flexibility. It will help with debug and will make your entire codebase more readable. Just write something like that and properly name it.

struct TwoTextViews: some View {
    @Binding var firstLabelText: String
    @Binding var secondLabelText: String

    var body: some View {
        VStack {
            Text(firstLabelText)
            Text(secondLabelText)
        }
    }
}

Don't really get the situation when you should return a list of separate views instead of them wrapped into some container.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM