Updated for Xcode 14.2
SwiftUI has a dedicated DisclosureGroup
view that presents a disclosure indicator and contains content inside. In its simplest form it can be controlled entirely by the user, but you can also bind it to a Boolean property to determine programmatically whether its content is currently visible or not.
For example, this creates a DisclosureGroup
with lots of text inside:
DisclosureGroup("Show Terms") {
Text("Long terms and conditions here long terms and conditions here long terms and conditions here long terms and conditions here long terms and conditions here long terms and conditions here.")
}
.frame(width: 300)
Download this as an Xcode project
If you wanted to track whether the disclosure group was opened or not, bind it to a Boolean like this:
struct ContentView: View {
@State private var revealDetails = false
var body: some View {
DisclosureGroup("Show Terms", isExpanded: $revealDetails) {
Text("Long terms and conditions here long terms and conditions here long terms and conditions here long terms and conditions here long terms and conditions here long terms and conditions here.")
}
.frame(width: 300)
}
}
Download this as an Xcode project
You can of course modify the Boolean’s state programmatically to control whether the group is expanded or not.
SAVE 50% To celebrate WWDC23, all our books and bundles are half price, so you can take your Swift knowledge further without spending big! Get the Swift Power Pack to build your iOS career faster, get the Swift Platform Pack to builds apps for macOS, watchOS, and beyond, or get the Swift Plus Pack to learn advanced design patterns, testing skills, and more.
Link copied to your pasteboard.