View ModifieriOS 18.0+macOS 15.0+tvOS 18.0+watchOS 11.0+visionOS 2.0+labelsVisibility(_:) in SwiftUI
Declaration
compose().labelsVisibility(visibility: SwiftUI.Visibility)nonisolated func labelsVisibility(_ visibility: Visibility) -> some ViewParameters
visibility*SwiftUI.Visibility
Discussion
Use this modifier when you want to omit a label from one or more
labeled content in your user interface. For example, the first Toggle
in the following example hides its label:
VStack {
Toggle(isOn: $toggle1) {
Text("Toggle 1")
}
.labelsVisibility(.hidden)
Toggle(isOn: $toggle2) {
Text("Toggle 2")
}
}The VStack in the example above centers the first toggle's control
element in the available space, while it centers the second toggle's
combined label and control element:
Always provide a label for controls, even when you hide the label, because SwiftUI uses labels for other purposes, including accessibility.
On iOS, a Picker within a Menu hides its label by default. You can use
this modifier to explicitly show the label in that context:
Menu {
Picker("Flavor", selection: $selectedFlavor) {
Text("Chocolate").tag(Flavor.chocolate)
Text("Vanilla").tag(Flavor.vanilla)
Text("Strawberry").tag(Flavor.strawberry)
}
.labelsVisibility(.visible)
}See also
More in Appearance.
- backgroundSets the view's background to a style.
- badgeGenerates a badge for the view from a localized string resource.
- badgeProminenceSpecifies the prominence of badges created by this view.
- borderAdds a border to this view with the specified style and width.
- buttonSizingThe preferred sizing behavior of buttons in the view hierarchy.
- containerBackgroundSets the container background of the enclosing container using a view.
- headerProminenceSets the header prominence for this view.
- hiddenHides this view unconditionally.
- imageScaleScales images within the view according to one of the relative sizes available including small, medium, and large images sizes.
- labelIconToTitleSpacingSet the spacing between the icon and title in labels.
- labelReservedIconWidthSet the width reserved for icons in labels.
- labelsHiddenHides the labels of any controls contained within this view.