Applying different LabelStyle conditionally fails

Viewed 31

I want my users to be able to define whether they want .iconOnly or .titleAndIcon behaviour for their label.

Unexpectedly to me though I am not able to apply on or the other style conditionally at the top of my view hierarchy:

enter image description here

Any suggestions on what needs to be done here?

1 Answers

Here is a possible approach. Tested with Xcode 13.4 / iOS 15.5

extension View {
    @ViewBuilder
    func labelStyle(includingText: Bool) -> some View {
        if includingText {
            self.labelStyle(.titleAndIcon)
        } else {
            self.labelStyle(.iconOnly)
        }
    }
}

and usage like

TabView {
 // .. your code
}
.labelStyle(includingText: labelStyleShowText)
Related