Declaration
compose().buttonSizing(sizing: SwiftUI.ButtonSizing)nonisolated func buttonSizing(_ sizing: ButtonSizing) -> some ViewParameters
sizing*SwiftUI.ButtonSizingA button sizing behavior that may be used to influence the primary axis size of buttons capable of adapting to it.
Discussion
Views may use the specified button sizing when determining the size they choose to be in their primary axis within their parent view's proposed size.
Many built-in controls that display as a button adapt to this view
modifier. For example, you can make certain styles of Button,
Picker, ControlGroup, and Toggle flexible by applying
this modifier to them or their container.
This example creates a button that spans the width of its container, which you may want to do if the button is placed in a narrow context, like the sidebar of a welcome window.
Button("Open Document…", action: openDocument)
.buttonSizing(.flexible)Your own views and styles can adapt to this view modifier by reading the
EnvironmentValues.buttonSizing environment value and applying an
appropriate frame.
struct CustomButtonStyle: ButtonStyle {
@Environment(\.buttonSizing) private var buttonSizing
private var maxWidth: CGFloat {
switch buttonSizing {
case .flexible: .infinity
case .fitted, _: nil
}
}
func makeBody(configuration: Configuration) -> some View {
configuration.content
.frame(maxWidth: maxWidth)
.background(.tint, in: Capsule())
}
}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.
- 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.
- labelsVisibilityControls the visibility of labels of any controls contained within this view.