HidingViews

1.1.1

Custom ViewModifier: `.isHidden(_:)` - Easily hide Views, controlled by a boolean. https://stackoverflow.com/a/59228385/9607863
GeorgeElsham/HidingViews

What's New

Clearer documentation

2021-03-03T10:42:17Z

This release provides clearer documentation over 1.1.0.

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:

Text("Hello World!")
    .isHidden(true)

Or:

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:

import SwiftUI


extension 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
        }
    }
}

Description

  • Swift Tools 5.2.0
View More Packages from this Author

Dependencies

  • None
Last updated: Fri Oct 13 2023 22:25:31 GMT-0900 (Hawaii-Aleutian Daylight Time)