contentMargins(_:for:)
Configures the content margin for a provided placement.
Declaration
nonisolated func contentMargins(_ length: CGFloat, for placement: ContentMarginPlacement = .automatic) -> some View
Parameters
- length:
The amount of margins to add on all edges.
- placement:
Where the margins should be added.
Discussion
Use this modifier to customize the content margins of different kinds of views. For example, you can use this modifier to customize the margins of scrollable views like ScrollView. In the following example, the scroll view will automatically inset its content by the safe area plus an additional 20 points on the leading and trailing edge.
ScrollView(.horizontal) {
// ...
}
.contentMargins(.horizontal, 20.0)You can provide a ContentMarginPlacement to target specific parts of a view to customize. For example, provide a scrollContent placement to inset the content of a TextEditor without affecting the insets of its scroll indicators.
TextEditor(text: $text)
.contentMargins(.horizontal, 20.0, for: .scrollContent)Similarly, you can customize the insets of scroll indicators separately from scroll content. Consider doing this when applying a custom clip shape that may clip the indicators.
ScrollView {
// ...
}
.clipShape(.rect(cornerRadius: 20.0))
.contentMargins(10.0, for: .scrollIndicators)When applying multiple contentMargins modifiers, modifiers with the same placement will override modifiers higher up in the view hierarchy.