Search code examples
swiftuiswiftui-list

Apply modifier on if else block


As seen in the code below, I have applied frame() modifier in the if block and else block.

var body: some View {
    NavigationView {
        if model.mode == .active {
            activeList
                .frame(minWidth: 200)
        } else {
            trashedList
                .frame(minWidth: 200)
        }
    }
}

How do I remove duplication of frame(minWidth:)?

More generally, is there a way where outcome of if else can have modifiers at a common place?


Solution

  • I can advice you to use Group as it doesn't change your layout such as :

    var body: some View {
        NavigationView {
            Group {
                if model.mode == .active {
                    activeList
                } else {
                    trashedList
                }
            }
            .frame(minWidth: 200)
        }
    }