Modifiers
background
Sets the background of a view.
background(color, shape)
| Parameter | Type | Description |
|---|---|---|
color | Color | The background color (hex string). For example, #FF0000. |
shape(optional) | Shape | Optional shape to clip the background. If not provided, the background will fill the entire view. |
Sets the background of a view.
Returns: ModifierConfig
See: Official SwiftUI documentation.
Works with
You can add background to any view. It changes what you see on 38 of the 39 components.
- VStack
- HStack
- ZStack
- LazyVStack
- LazyHStack
- Group
- Divider
- Overlay
- ScrollView
- Button
- Toggle
- Checkbox
- Slider
- Picker
- DatePicker
- ColorPicker
- Link
- ControlGroup
- Menu
- ContextMenu
- TextField
- SecureField
- Text
- Label
- Image
- DisclosureGroup
- SwipeActions
- BottomSheet
- Alert
- ConfirmationDialog
- Popover
- ProgressView
- Gauge
- TabView
- AccessoryWidgetBackground
Needs care
It works on these components, but not in the way that you may expect.
| Component | What to know |
|---|---|
| List | List and Form draw their own background on top. Add scrollContentBackground('hidden') to see it. |
| Form | List and Form draw their own background on top. Add scrollContentBackground('hidden') to see it. |
| Section | It changes the whole section, not one row. For one row, put it on the view in the row. |
No visible effect
These components accept it, but you see no change.
| Component | Why nothing happens |
|---|---|
| Spacer | It paints in the area of the view, and Spacer draws nothing that you can see. |
Sources
The original Expo docs for this page:
- background (Modifiers) (
source-of-truth/versions/v57.0.0/sdk/ui/swift-ui/modifiers.md)