DisclosureGroup

A view that shows or hides another content view, based on the state of a disclosure control.

ComponentiOS 14.0+macOS 11.0+DisclosureGroup in SwiftUI

Declaration

React
import { DisclosureGroup } from 'swiftui-react-native';
Swift
nonisolated struct DisclosureGroup<Label, Content> where Label : View, Content : View

Initializers

6 SwiftUI initializers back <DisclosureGroup>. The props you pass pick one.

React
<DisclosureGroup
  title: string
>
  children?: ReactNode
</DisclosureGroup>
Swift
nonisolated init(
    _ titleKey: LocalizedStringKey,
    @ContentBuilder content: @escaping () -> Content
)
  • title*string

    The key for the localized label of self that describes the content of the disclosure group.

    Swift titleKey: LocalizedStringKey

  • childrenReactNode

    Content. Strings become Text; React Native views are laid out by Yoga and placed by SwiftUI.

    Swift content: () -> Content

Also takes the common view props: modifiers, style and testID.

Overview

A disclosure group view consists of a label to identify the contents, and a control to show and hide the contents. Showing the contents puts the disclosure group into the "expanded" state, and hiding them makes the disclosure group "collapsed".

In the following example, a disclosure group contains two toggles and an embedded disclosure group. The top level disclosure group exposes its expanded state with the bound property, topLevelExpanded. By expanding the disclosure group, the user can use the toggles to update the state of the toggleStates structure.

struct ToggleStates {
    var oneIsOn: Bool = false
    var twoIsOn: Bool = true
}
@State private var toggleStates = ToggleStates()
@State private var topExpanded: Bool = true

var body: some View {
    DisclosureGroup("Items", isExpanded: $topExpanded) {
        Toggle("Toggle 1", isOn: $toggleStates.oneIsOn)
        Toggle("Toggle 2", isOn: $toggleStates.twoIsOn)
        DisclosureGroup("Sub-items") {
            Text("Sub-item 1")
        }
    }
}

From Apple’s SwiftUI documentation. Code samples there are Swift.

Relationships

Conforms to

  • View

See also

More in Controls.