Flatten TupleViews using SwiftUI

后端 未结 2 1177
一整个雨季
一整个雨季 2021-01-13 19:38

Ok, SwiftUI was released this week so we\'re all n00bs but... I have the following test code:

var body: some View {
    switch shape {
    case .oneCircle:
          


        
相关标签:
2条回答
  • 2021-01-13 19:46

    You can also use Group which is logical container so won't change anything visual.

     var body: some View {
        Group {
         switch shape {
         case .oneCircle:
            return ZStack {
                Circle().fill(Color.red)
            }
         case .twoCircles:
            return ZStack {
                Circle().fill(Color.green)
                Circle().fill(Color.blue)
            }
         }
        }
    }
    
    0 讨论(0)
  • 2021-01-13 20:01

    One way to fix this is to use the type eraser AnyView:

    var body: some View {
        switch shape {
        case .oneCircle:
            return AnyView(ZStack {
                Circle().fill(Color.red)
            })
        case .twoCircles:
            return AnyView(ZStack {
                Circle().fill(Color.green)
                Circle().fill(Color.blue)
            })
        }
    }
    
    0 讨论(0)
提交回复
热议问题