Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Using environmental size classes #20

Closed
dkk opened this issue Jul 17, 2023 · 1 comment
Closed

Using environmental size classes #20

dkk opened this issue Jul 17, 2023 · 1 comment

Comments

@dkk
Copy link

dkk commented Jul 17, 2023

Is there a way to use environmental size classes when defining a custom TabItemStyle?

I would like to show text bellow the icon when the app has a .regular .horizontalSizeClass, and only show the icon when it is .compact. With the current implementation I'm not really sure how to do it in a way that the size class variable gets updated correctly and I don't get "Accessing Environment<Optional<UserInterfaceSizeClass>>'s value outside of being installed on a View. This will always read the default value and will not update."

@dkk
Copy link
Author

dkk commented Jul 18, 2023

I just found a way.

Hold the @Environment(\.horizontalSizeClass) var sizeClass in the View where you add the TabBar and then just:

TabBar {
/// ... snip ...
}
.if(sizeClass == .regular, {
    $0.tabItem(style: CustomRegularStyle())
}, else: {
    $0.tabItem(style: CustomCompactStyle())
} // I hope this custom modifier is self-explanatory

@dkk dkk closed this as completed Jul 18, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant