Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/GeorgeElsham/HidingViews
Custom ViewModifier: `.isHidden(_:)` - Easily hide Views, controlled by a boolean. https://stackoverflow.com/a/59228385/9607863
https://github.com/GeorgeElsham/HidingViews
Last synced: 3 months ago
JSON representation
Custom ViewModifier: `.isHidden(_:)` - Easily hide Views, controlled by a boolean. https://stackoverflow.com/a/59228385/9607863
- Host: GitHub
- URL: https://github.com/GeorgeElsham/HidingViews
- Owner: GeorgeElsham
- License: other
- Created: 2020-01-15T08:17:09.000Z (about 5 years ago)
- Default Branch: main
- Last Pushed: 2023-09-28T00:37:42.000Z (over 1 year ago)
- Last Synced: 2024-08-05T23:26:54.988Z (7 months ago)
- Language: Swift
- Homepage:
- Size: 30.3 KB
- Stars: 41
- Watchers: 3
- Forks: 5
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE.txt
Awesome Lists containing this project
README
# HidingViews
This is a custom ViewModifier, `.isHidden(_:)`. It allows a View to be hidden, controlled by a boolean property/value.
This project is an example of how to use it. Use it like so:
```swift
Text("Hello World!")
.isHidden(true)
```
Or:```swift
Text("Label")
.isHidden(true, remove: true)
```
`@State` can come in very useful for this situation to actively change whether a view is hidden, for example controlled by a `Toggle`.---
If you are just interested in the code for it to create the modifier, here it is:```swift
import SwiftUIextension View {
/// Hide or show the view based on a boolean value.
///
/// Example for visibility:
///
/// Text("Label")
/// .isHidden(true)
///
/// Example for complete removal:
///
/// Text("Label")
/// .isHidden(true, remove: true)
///
/// - Parameters:
/// - hidden: Set to `false` to show the view. Set to `true` to hide the view.
/// - remove: Boolean value indicating whether or not to remove the view.
@ViewBuilder func isHidden(_ hidden: Bool, remove: Bool = false) -> some View {
if hidden {
if !remove {
self.hidden()
}
} else {
self
}
}
}
```