I have overlay with below code
.overlay(abcViewModel.isError ? AnyView(DialogBoxOneButton(accessibilityID: "dialogboxes", dialogboxType: .error, dialogboxTitle: "title", dialogboxBody: "body", dialogboxFirstButtonTitle: "title", dialogboxFirstButtonCallback: {
action()
abcViewModel.isError = false
}, isDialogboxFirstButtonClicked: .constant(true))) : AnyView(EmptyView()))
I am trying to avoid use of Anyview and did below code but getting error as "Unknown attribute ‘Viewbuilder’" and not sure how to return view in if case
private func getOverlayView() -> some View {
if abcViewModel.isError {
DialogBoxOneButton(accessibilityID: "dialogboxes", dialogboxType: .error, dialogboxTitle: "title", dialogboxBody: "body", dialogboxFirstButtonTitle: "title", dialogboxFirstButtonCallback: {
action()
abcViewModel.isError = false
}, isDialogboxFirstButtonClicked: .constant(true))
} else {
return EmptyView()
}
}
Kindly help me the best way for this
2
Answers
You need to add
@ViewBuilder
to the computed property/function creating the overlay view.and then pass this property to
overlay
You can add
@ViewBuilder
above your function signature like:And get rid of all
return
keywords.💡 Cleaner method
You can extend the
View
and define a simple function and hide any view conditionally like:and use it like: