A type that represents part of your app’s user interface and provides modifiers that you use to configure views.
- iOS 13.0+
- macOS 10.15+
- Mac Catalyst 13.0+
- tvOS 13.0+
- watchOS 6.0+
- Xcode 11.0+
You create custom views by declaring types that conform to the
View protocol. Implement the required
body computed property to provide the content for your custom view.
Assemble the view’s body by combining one or more of the primitive views provided by SwiftUI, like the
Text instance in the example above, plus other custom views that you define, into a hierarchy of views.
View protocol provides a large set of modifiers, defined as protocol methods with default implementations, that you use to position and configure views in the layout of your app. Modifiers typically work by wrapping the view instance on which you call them in another view with the specified characteristics. For example, adding the
opacity(_:) modifier to a text view returns a new view with some amount of transparency:
The effects of a modifier typically propagate to any child views that don’t explicitly override the modifier. For example, a
VStack instance on its own acts only to vertically stack other views, and has no text to display. Therefore, a
font(_:) modifier that you apply to the stack has no effect on the stack itself. Yet the font modifier does apply to any of the stack’s child views, some of which might display text. On the other hand, you can locally override the stack’s modifier by adding another to a specific child view:
You commonly chain modifiers, each wrapping the result of the previous one. For example, you can wrap a text view in an invisible box with a given width using the
frame(width: modifier to influence its layout, and then use the
border(_: modifier to draw an outline around that:
The order in which you apply modifiers matters. For example, the border that results from the above code outlines the full width of the frame.
If you instead apply the border first, it outlines the text view, which never takes more space than it needs to render its contents.
Wrapping that view in another invisible one with a fixed 100 point width affects the layout of the composite view, but has no effect on the border.