Displaying an empty view in SwiftUI

前端 未结 3 2298
情话喂你
情话喂你 2021-02-20 18:02

In SwiftUI there\'s frequently a need to display an \"empty\" view based on some condition, e.g.:

struct OptionalText:          


        
3条回答
  •  忘掉有多难
    2021-02-20 18:42

    You can use the @ViewBuilder. Then you don't even need an EmptyView:

    @ViewBuilder
    var body: some View {
        if let text = text {
            Text(text)
        }
    }
    

    Note than you don't return anything, with a @ViewBuilder you just build your view.

提交回复
热议问题