View
public protocol View
A type that represents a view.
-
Declaration
Swift
var viewStore: ViewValues { get set } -
Declaration
Swift
var body: View { get } -
background(_:Extension method) Declaration
Swift
public func background(_ color: Color?) -> Self -
padding(_:Extension method_: ) Creates a view that pads this view using the specified edge instets with a specified value.
Declaration
Swift
public func padding(_ edges: Edge.Set = .all, _ length: CGFloat? = nil) -> PaddingViewParameters
edgesThe set of edges along which to inset this view.
lengthThe amount to inset this view on each edge. If
nil, the amount is the system default amount.Return Value
A view that pads this view using the edge insets you specify.
-
padding(_:Extension method) Creates a view that pads this view using the values inside of
EdgeInsets.Declaration
Swift
public func padding(_ insets: EdgeInsets) -> PaddingViewReturn Value
A view that pads this view using the edge insets you specify.
-
padding(_:Extension method) Creates a view that pads this view along all edge insets by the amount you specify.
Declaration
Swift
public func padding(_ length: CGFloat) -> PaddingViewParameters
lengthThe amount to inset this view on each edge.
Return Value
A view that pads this view by the amount you specify.
-
padding()Extension methodCreates a view that pads this view along all edges by a default amount.
Declaration
Swift
public func padding() -> PaddingViewReturn Value
A view that pads this view using the edge insets you specify.
-
frame(width:Extension methodheight: ) Sets the frame of a view with specified width and/or height.
Pass at least one size parameter. Any
nilor unspecified dimensions won’t be applied to the frame.Specified dimensions will set the exact width or height of the view with required priority. You are responsible for making sure that the constraints don’t conflict with other views’ constraints.
Declaration
Swift
public func frame(width: CGFloat? = nil, height: CGFloat? = nil) -> SelfParameters
widthThe width of the view’s frame.
heightThe height of the view’s frame.
Return Value
The view with modified frame.
-
frame(minWidth:Extension methodmaxWidth: minHeight: maxHeight: ) Sets the frame of a view with specified max/min width and/or height.
Pass at least one size parameter. Any
nilor unspecified dimensions won’t be applied to the frame.Specify minimum or maximum dimensions when you want to constraint the frame of a view that can dynamically change it’s size because of parent, child or sibling views.
Tip: Pass .infinity to either maxWidth or maxHeight when you want the view’s dimension to stretch as far as it’s parent allows it to.
Declaration
Swift
public func frame(minWidth: CGFloat? = nil, maxWidth: CGFloat? = nil, minHeight: CGFloat? = nil, maxHeight: CGFloat? = nil) -> SelfParameters
minWidthThe minimum width of the resulting frame.
maxWidthThe maximum width of the resulting frame.
minHeightThe minimum height of the resulting frame.
maxHeightThe maximum height of the resulting frame.
Return Value
The view with modified frame.
-
frame()Extension methodThis is to inform invalid case.
frame()should always be called with at least one parameter.Declaration
Swift
@available(*, deprecated, message: "Please pass one or more parameters.") public func frame() -> Self -
contextMenu(menuItems:Extension method) Adds a contextual menu to this view.
Use contextual menus to add actions that change depending on the user’s current focus and task.
Important: Only Buttons with
ImageorTextare allowed. The following 3 view combinations are allowed for building a contextual menu:Text("Control Click Me") .contextMenu { // First combination Button(Text("Add")) {} // Second combination Button(action: {}) { Image() } // Third combination Button(action: {}) { Text("Add") Image() } }Declaration
Swift
@available(tvOS, unavailable) public func contextMenu(@ViewBuilder menuItems: () -> View) -> SelfReturn Value
The view with a contextual menu.
-
contextMenu(_:Extension method) Attaches a
ContextMenuand its children toself.This modifier allows for the contextual menu to be conditionally available by passing
nilas the value forcontextMenu.Declaration
Swift
@available(tvOS, unavailable) public func contextMenu(_ contextMenu: ContextMenu?) -> Self -
buttonStyle(_:Extension method) Declaration
Swift
public func buttonStyle<S>(_ style: S) -> Self where S : ButtonStyle -
overlay(_:Extension methodalignment: ) Layers a secondary view in front of this view.
When you apply an overlay to a view, the original view continues to provide the layout characteristics for the resulting view. For example, the layout for the caption in this view fits within the width of the image:
Image(name: "artichokes") .overlay( HStack { Text("Artichokes"), // Text to use as a caption. Spacer() } .padding() .foregroundColor(.white) .background(Color.black.opacity(0.5)), alignment: .bottom )Declaration
Swift
public func overlay<Overlay>(_ overlay: Overlay, alignment: Alignment = .center) -> Self where Overlay : ViewParameters
overlayThe view to layer in front of this view.
alignmentThe alignment for
overlayin relation to this view.Return Value
The view with an
overlaylayer in front. -
border(_:Extension methodwidth: ) Adds a border to this view with the specified style and width.
By default, the border appears inside the bounds of this view. In this example, the four-point border covers the text:
Text("Artichokes") .font(.title) .border(Color.green, width: 4)To place a border around the outside of this view, apply padding of the same width before adding the border:
Text("Artichokes") .font(.title) .padding(4) .border(Color.green, width: 4)Declaration
Swift
public func border(_ content: Color, width: CGFloat = 1) -> SelfParameters
contentThe border style.
widthThe thickness of the border.
Return Value
The view with a border with the specified style and width to this view.
-
aspectRatio(contentMode:Extension method) Sets the view’s content mode only when it’s resizable.
Image() .resizable() .aspectRatio(contentMode: .fit) .frame(width: 200, height: 200)Declaration
Swift
public func aspectRatio(contentMode: ContentMode) -> SelfParameters
contentModeA flag indicating whether this view should fit or fill the parent context.
Return Value
The view with modified content mode.
-
scaledToFit()Extension methodScales this view to fit its parent.
This view’s aspect ratio is maintained as the view scales. This method is equivalent to calling
aspectRatio(contentMode: .fit).Image() .resizable() .scaledToFit() .frame(width: 200, height: 200)Declaration
Swift
public func scaledToFit() -> SelfReturn Value
The view with fit content mode.
-
scaledToFill()Extension methodScales this view to fill its parent.
This view’s aspect ratio is maintained as the view scales. This method is equivalent to calling
aspectRatio(contentMode: .fill).Image() .resizable() .scaledToFill() .frame(width: 200, height: 200)Declaration
Swift
public func scaledToFill() -> SelfReturn Value
The view with fill content mode.
-
foregroundColor(_:Extension method) Set the foreground color within
self.Declaration
Swift
public func foregroundColor(_ color: Color?) -> Self -
edgesIgnoringSafeArea(_:Extension method) Changes the constraints of this view so that it matches the window on the specified edges. Important: Don’t use this on views that don’t touch the edges of the safe area on the edges that you want to ignore.
Declaration
Swift
public func edgesIgnoringSafeArea(_ edges: Edge.Set) -> SelfParameters
edgesThe set of the edges in which to expand the size proposed for this view.
Return Value
The view with changed constraints.
-
accentColor(_:Extension method) Sets the accent color for this view and the views it contains.
The system uses the accent color for common controls and containers. For example, a button’s label might use the accent color for its text.
Declaration
Swift
public func accentColor(_ accentColor: Color?) -> SelfParameters
accentColorThe color to use as an accent color. If
nil, the accent color is the system default -
disabled(_:Extension method) Adds a condition that controls whether users can interact with this view.
Disabling a view will also disable all of its children, regardless of the children being disabled or not. For example, the following button is not interactable
HStack { Button(Text("Action")) {} .disabled(true) } .disabled(false)Declaration
Swift
public func disabled(_ disabled: Bool) -> SelfParameters
disabledA Boolean value that determines whether users can interact with this view.
-
opacity(_:Extension method) Sets the transparency of this view.
Apply opacity to reveal views that are behind another view or to de-emphasize a view.
When applying the
opacity(_:)modifier to a view that already has an opacity, the modifier supplements—rather than replaces—the view’s opacity.Declaration
Swift
public func opacity(_ opacity: Double) -> SelfParameters
opacityA value between 0 (fully transparent) and 1 (fully opaque).
-
clipped(antialiased:Extension method) Clips this view to its bounding rectangular frame.
By default, a view’s bounding frame is used only for layout, so any content that extends beyond the edges of the frame is still visible. Use the
clipped()modifier to hide any content that extends beyond these edges.Declaration
Swift
public func clipped(antialiased: Bool = false) -> SelfParameters
antialiasedA Boolean value that indicates whether smoothing is applied to the edges of the clipping rectangle.
-
cornerRadius(_:Extension methodantialiased: ) Clips this view to its bounding frame, with the specified corner radius.
By default, a view’s bounding frame only affects its layout, so any content that extends beyond the edges of the frame remains visible. Use the
cornerRadius()modifier to hide any content that extends beyond these edges while applying a corner radius.The following code applies a corner radius of 20 to a square image:
Image(name: "square") .cornerRadius(20)Declaration
Swift
public func cornerRadius(_ radius: CGFloat, antialiased: Bool = true) -> SelfParameters
antialiasedA Boolean value that indicates whether smoothing is applied to the edges of the clipping rectangle.
-
shadow(color:Extension methodradius: x: y: ) Adds a shadow to this view.
Tip: If you want to apply a shadow on a clipped view, you must apply to its parent view instead. Example:
Image(name: "square") .cornerRadius(20) .padding(0) .shadow(radius: 2)Declaration
Parameters
colorThe shadow’s color.
radiusThe shadow’s size.
xA horizontal offset you use to position the shadow relative to this view.
yA vertical offset you use to position the shadow relative to this view.
-
font(_:Extension method) Sets the default font for text in this view.
Declaration
Swift
public func font(_ font: Font?) -> SelfParameters
fontThe default font to use in this view.
-
keyboardType(_:Extension method) Sets the keyboard type for text input controls in this view.
Declaration
Swift
@available(macOS, unavailable) @available(watchOS, unavailable) public func keyboardType(_ type: UIKeyboardType) -> Self -
navigationBarHidden(_:Extension method) Hides the navigation bar for this view.
This modifier only takes effect when this view is inside of and visible within a
NavigationView.Declaration
Swift
public func navigationBarHidden(_ hidden: Bool) -> SelfParameters
hiddenA Boolean value that indicates whether to hide the navigation bar.
-
statusBar(hidden:Extension method) Hides the status bar for this view.
This modifier only takes effect when this view is visible.
Declaration
Swift
public func statusBar(hidden: Bool) -> Self -
statusBarStyle(_:Extension method) Sets the status bar style for this view.
Note: Not available in SwiftUI.
Declaration
Swift
public func statusBarStyle(_ style: UIStatusBarStyle) -> Self -
lineLimit(_:Extension method) Sets the maximum number of lines that text can occupy in this view.
The line limit applies to all
Textinstances within this view. For example, anHStackwith multiple pieces of text longer than three lines caps each piece of text to three lines rather than capping the total number of lines across theHStack.Note
a non-nil
numberless than 1 will be treated as 1.Declaration
Swift
public func lineLimit(_ number: Int?) -> SelfParameters
numberThe line limit. If
nil, no line limit applies. -
onTapGesture(perform:Extension method) Returns a version of
selfthat will invokeactionafter recognizing a tap gesture.Declaration
Swift
public func onTapGesture(perform action: @escaping () -> Void) -> Self -
layoutPriority(_:Extension method) Sets the priority by which a parent layout should apportion space to this child.
The default priority is
0. In a group of sibling views, raising a view’s layout priority encourages that view to shrink later when the group is shrunk and stretch sooner when the group is stretched.A parent layout should offer the child(ren) with the highest layout priority all the space offered to the parent minus the minimum space required for all its lower-priority children, and so on for each lower priority value.
Declaration
Swift
public func layoutPriority(_ value: Double) -> Self -
labelsHidden()Extension methodHides labels for controls that contain a descriptive label by default.
Declaration
Swift
public func labelsHidden() -> Self -
tag(_:Extension method) -
tabItem(_:Extension method) Sets the tab item information for the tab root view Important: You must provide both one Text and Image only.
Declaration
Swift
public func tabItem(@ViewBuilder _ label: () -> View) -> Self -
multilineTextAlignment(_:Extension method) Sets the alignment on text when spanning multiple lines.
Declaration
Swift
public func multilineTextAlignment(_ alignment: TextAlignment) -> Self -
onAppear(perform:Extension method) Executes the action when the view appears on screen.
Declaration
Swift
public func onAppear(perform action: (() -> Void)? = nil) -> Self -
onDisappear(perform:Extension method) Executes the action when the view disappears from the screen.
Declaration
Swift
public func onDisappear(perform action: (() -> Void)? = nil) -> Self -
modifier(_:Extension method) Returns a modified version of the view, which is modified by the
modifierparameter.Declaration
Swift
public func modifier(_ modifier: ViewModifier) -> View -
mask(_:Extension method) Masks the view with another view. The view passed as parameter will act as the mask.
Declaration
Swift
public func mask(_ mask: View) -> Self -
coordinateSpace(_:Extension method) Sets the coordinate space name for this view. GeometryProxy types can identify this view’s coordinate space by the name set, when resolving frames.
Declaration
Swift
public func coordinateSpace(_ name: String) -> Self -
highPriorityGesture(_:Extension method) Attaches
gesturetoselfsuch that it has higher precedence than gestures defined byself.Declaration
Swift
public func highPriorityGesture<T>(_ gesture: T) -> Self where T : Gesture -
simultaneousGesture(_:Extension method) Attaches
gestureto self such that it will be processed simultaneously with gestures defined byself.Declaration
Swift
public func simultaneousGesture<T>(_ gesture: T) -> Self where T : Gesture -
geometryListener(_:Extension method) Updates the
listenerwith the view’s geometry proxy.Updates in the view layout will update the value contained in the
readerbinding. Use this when you want to reference the view’s frame or size in any coordinate space.Declaration
Swift
public func geometryListener(_ listener: Binding<GeometryProxy>) -> Self -
listRowInsets(_:Extension method) Defines the edge insets of a List’s cells.
Pass an empty EdgeInsets value when you want to remove the default cell left padding. Important: You must set the
listRowInsetsin the topmost view element in the hierarchy of a List’s cell. Example:List(myData) { data in VStack { Text("Element \(data.name)") } .listRowInsets(EdgeInsets()) }Declaration
Swift
public func listRowInsets(_ insets: EdgeInsets?) -> Self -
textContentType(_:Extension method) Sets the semantic meaning for all text entry elements in the hierarchy. Functions such as keyboard suggestions will show up depending on the content type and user data.
Declaration
Swift
public func textContentType(_ textContentType: UITextContentType?) -> Self -
onDrag(dragBegan:Extension methoddragEnded: data: ) DragBegan and DragEnd closures are added. Not in accordance with SwiftUI Compatibility.
Activates this view as the source of a drag and drop operation.
Applying the
onDrag(_:)modifier adds the appropriate gestures for drag and drop to this view. When a drag operation begins, a rendering of this view is generated and used as the preview image.Declaration
Swift
@available(tvOS, unavailable) @available(watchOS, unavailable) public func onDrag(dragBegan: (() -> Void)? = nil, dragEnded: ((UIDropOperation) -> Void)? = nil, data: @escaping () -> NSItemProvider) -> SelfParameters
dataA closure that returns a single doc://com.apple.documentation/documentation/Foundation/NSItemProvider that represents the draggable data from this view.
dragBeganCalled when a drag and drop gesture begins. Not SwiftUI compatible.
dragEndedCalled when a drag and drop gesture ends. Not SwiftUI compatible.
Return Value
A view that activates this view as the source of a drag and drop operation, beginning with user gesture input.
-
onDrop(of:Extension methodisTargeted: perform: ) Defines the destination for a drag and drop operation, using the same size and position as this view, handling dropped content with the given closure.
Declaration
Swift
public func onDrop(of supportedTypes: [String], isTargeted: Binding<Bool>?, perform action: @escaping ([NSItemProvider]) -> Bool) -> SelfParameters
supportedTypesThe uniform type identifiers that describe the types of content this view can accept through drag and drop. If the drag and drop operation doesn’t contain any of the supported types, then this drop destination doesn’t activate and
isTargeteddoesn’t update.isTargetedA binding that updates when a drag and drop operation enters or exits the drop target area. The binding’s value is
truewhen the cursor is inside the area, andfalsewhen the cursor is outside.actionA closure that takes the dropped content and responds appropriately. The parameter to
actioncontains the dropped items, with types specified bysupportedTypes. Returntrueif the drop operation was successful; otherwise, returnfalse.Return Value
A view that provides a drop destination for a drag operation of the specified types.
-
strictHighPerformanceUpdate()Extension methodStrict updates during high performance rendering will update this view but not its children views. This should be used for collection types when you want to get a slight performance gain by preventing children from updating.
One example for using this, is if you want to modify the opacity of a collection of views. Setting this value will modify the opacity of the group, affecting all children views equally without updating them individually.
VStack { Text("One") Text("Two") } .opacity(offset.y) .strictHighPerformanceUpdate()See
High Performance Updatesin the documentation for more information.Important
Not SwiftUI compatible.Declaration
Swift
public func strictHighPerformanceUpdate() -> Self -
skipHighPerformanceUpdate()Extension methodSkip update rendering on this view completely during high performance rendering.
See
High Performance Updatesin the documentation for more information.Important
Not SwiftUI compatible.Declaration
Swift
public func skipHighPerformanceUpdate() -> Self
-
navigationBarTitle(_:Extension methoddisplayMode: ) Sets the title of the navigation bar
Declaration
Swift
public func navigationBarTitle(_ title: String, displayMode: NavigationBarTitleDisplayMode) -> Self -
navigationBarItems(leading:Extension methodtrailing: ) Sets leading and trailing views. AltSwiftUI: Only
Button<Text>,Button<Image>andHStackwith data ofButtontype is allowed.Declaration
Swift
public func navigationBarItems(leading: View, trailing: View) -> Self -
navigationBarItems(leading:Extension method) Sets leading view. AltSwiftUI: Only
Button<Text>,Button<Image>andHStackwith data ofButtontype is allowed.Declaration
Swift
public func navigationBarItems(leading: View) -> Self -
navigationBarItems(trailing:Extension method) Sets trailing view. AltSwiftUI: Only
Button<Text>,Button<Image>andHStackwith data ofButtontype is allowed.Declaration
Swift
public func navigationBarItems(trailing: View) -> Self -
sheet(isPresented:Extension methodisFullScreen: onDismiss: content: ) Presents a sheet.
Declaration
Swift
public func sheet<Content>(isPresented: Binding<Bool>, isFullScreen: Bool = false, onDismiss: (() -> Void)? = nil, content: @escaping () -> Content) -> View where Content : ViewParameters
isPresentedA
Bindingto whether the sheet is presented.onDismissA closure executed when the sheet dismisses.
contentA closure returning the content of the sheet.
-
alert(isPresented:Extension methodonForegroundView: content: ) Presents an alert.
Important
onForegroundViewis not a native SwiftUI featureDeclaration
-
actionSheet(isPresented:Extension methodonForegroundView: content: ) Presents an action sheet.
Important
onForegroundViewis not a native SwiftUI featureDeclaration
Swift
@available(macOS, unavailable) public func actionSheet(isPresented: Binding<Bool>, onForegroundView: Bool = false, content: () -> ActionSheet) -> SelfParameters
isPresentedA
Bindingto whether the action sheet should be shown.onForegroundViewA
Boolindicating whether the alert should be displayed from the foremost view or not.contentA closure returning the
ActionSheetto present. -
appStoreOverlay(isPresented:Extension methodconfiguration: ) Presents a StoreKit overlay when a given condition is true.
You use
appStoreOverlayto display an overlay that recommends another app. The overlay enables users to instantly view the other app’s page on the App Store.When
isPresentedis true, the system will runconfigurationto determine how to configure the overlay. The overlay will automatically be presented over the current scene.See also
SKOverlay.Configuration.Declaration
Swift
@available(iOS 14.0, *) @available(macCatalyst, unavailable) @available(macOS, unavailable) @available(tvOS, unavailable) @available(watchOS, unavailable) public func appStoreOverlay(isPresented: Binding<Bool>, configuration: @escaping () -> SKOverlay.Configuration) -> SelfParameters
isPresentedA Binding to a boolean value indicating whether the overlay should be presented.
configurationA closure providing the configuration of the overlay.
-
environmentObject(_:Extension method) Sets an environment object as a global object. Views can access this object through the
EnvironmentObjectproperty wrapper and listen to changes in anObservableObject.Declaration
Swift
public func environmentObject<B>(_ bindable: B) -> Self where B : ObservableObject
-
transformEffect(_:Extension method) Applies a transform effect to the view
Declaration
Swift
public func transformEffect(_ transform: CGAffineTransform) -> Self -
offset(_:Extension method) Applies a location offset based on a size. Modifying a view’s offset won’t affect its layout or related views’ layout.
Declaration
Swift
public func offset(_ offset: CGSize) -> Self -
offset(x:Extension methody: ) Applies a location offset based on a
xandy. Modifying a view’s offset won’t affect its layout or related views’ layout.Declaration
Swift
public func offset(x: CGFloat = 0, y: CGFloat = 0) -> Self -
rotationEffect(_:Extension method) Applies a rotation effect. Modifying a view’s rotation won’t affect its layout or related views’ layout.
Declaration
Swift
public func rotationEffect(_ angle: Angle) -> Self -
scaleEffect(_:Extension method) Applies a scale effect based on a size scale. Modifying a view’s scale won’t affect its layout or related views’ layout.
Declaration
Swift
public func scaleEffect(_ scale: CGSize) -> Self -
scaleEffect(_:Extension method) Applies a scale effect based on a scale for both width and height. Modifying a view’s scale won’t affect its layout or related views’ layout.
Declaration
Swift
public func scaleEffect(_ s: CGFloat) -> Self
-
animation(_:Extension method) Sets the animation to affect all previously defined view properties.
Setting a value of
nilwill impose impose no animation to the properties even if a value was changed inside awithAnimationclosure.Animations can be set to different subset of properties. Example:
Text("Example") .offset(x: 5) .animation(nil) .scale(0.5) .animation(.default)Declaration
Swift
public func animation(_ animation: Animation?) -> Self -
transition(_:Extension method) Sets a transition for when a view appears and disappears.
Important: In order for transitions to be animated, either modify a property inside a
withAnimationclosure or set a.animation()property to the view.Setting a custom animation as part of a custom transition definition will not trigger an animation, but will override existing
withAnimationanimations. This is the priority of animations applied to transitions:.animation() -> AnyTransition.animation() -> withAnimationAppearing events happen when a view is conditionally not included in a hierarchy, and conditionally removed from it. Example:
VStack { if showExample { Text("Example") .transition(.opacity) } }Declaration
Swift
public func transition(_ t: AnyTransition) -> Self
-
accessibility(identifier:Extension method) Uses the specified string to identify the view.
Declaration
Swift
public func accessibility(identifier: String) -> Self
View on GitHub
View Protocol Reference