Using disclosure groups to hide and show content
DisclosureGroup
is a view that's used to show or hide content based on the state of a disclosure control. It takes two parameters: a label
to identify its content and a binding
that controls whether the content is visible or hidden. Let's take a closer look at how it works by creating an app that shows and hides content in a disclosure group.
Getting ready
Create a new SwiftUI project and name it DisclosureGroups
.
How to do it…
We will create an app that uses the DisclosureGroup
view to view some planets in our solar system, continents on Earth, and some surprise text. The steps are as follows:
- Below the
ContentView
struct, add a state property calledshowplanets
:struct ContentView: View { @State private var showplanets = true // the rest of the content here }
- Replace the
Text
view in thebody
struct with aVStack
and aDisclosureGroup
view that contains twoText
views...