View

public extension View

Conditional Modifier

  • Runs a transform block, but only if the condition is met, otherwise the view remains unchanged.

    The transform can apply a modifier on the view.

    myView.if(X) { $0.padding(8) }
    

    See also: https://fivestars.blog/swiftui/conditional-modifiers.html

    Declaration

    Swift

    @ViewBuilder
    @MainActor
    func `if`(_ condition: Bool, transform: (Self) -> some View) -> some View

    Parameters

    condition

    The condition which have to be true to get the transform block called.

    transform

    The transform which should apply the modifier on the view.

    Return Value

    Either the new view with the modifier applied or the unmodified view.

  • Runs one or the the other transform block depending on a condition. The transform can apply a modifier on the view.

    myView.if(X) { $0.padding(8) } else: { $0.background(Color.blue) }
    

    See also: https://fivestars.blog/swiftui/conditional-modifiers.html

    Declaration

    Swift

    @ViewBuilder
    @MainActor
    func `if`(
    	_ condition: Bool,
    	if ifTransform: (Self) -> some View,
    	else elseTransform: (Self) -> some View
    ) -> some View

    Parameters

    condition

    The condition which decides which transform block should be called.

    ifTransform

    The transform block which gets called when the condition is true.

    elseTransform

    The transform block which gets called when the condition is false.

    Return Value

    The new view with one of the modifiers applied.

  • Runs a transform block when an optional could be successfully unwrapped. The transform can then access the unwrapped optional safely and apply any modifer to the view.

    myView.ifLet(optionalColor) { $0.foregroundColor($1) }
    

    See also: https://fivestars.blog/swiftui/conditional-modifiers.html

    Declaration

    Swift

    @ViewBuilder
    @MainActor
    func ifLet<Value>(_ value: Value?, transform: (Self, Value) -> some View) -> some View

    Parameters

    value

    The optional.

    transform

    The transform block called with the unwrapped value.

    Return Value

    The new view with the modifier applied or the unchanged view.

  • Same as the frame(width:height:alignment:) modifier, but with a size provided which maps to width and height.

    Declaration

    Swift

    @MainActor
    func frame(size: CGSize, alignment: Alignment = .center) -> some View

    Parameters

    size

    The size which will be provided to the frame(width:height) modifier by unwrapping the size’s widht and height.

    alignment

    The alignment of this view inside the resulting view. alignment applies if this view is smaller than the size given by the resulting frame.

    Return Value

    A view with fixed dimensions of width and height.

  • Executes the action when the view appears the first time. Similar to SwiftUI’s onAppear, but fires only once.

    Declaration

    Swift

    @MainActor
    func onFirstAppear(_ action: @escaping () -> Void) -> some View
  • Presents an activity sheet when the associated ActivityItem is present

    The system provides several standard services, such as copying items to the pasteboard, posting content to social media sites, sending items via email or SMS, and more. Apps can also define custom services.

    Declaration

    Swift

    @MainActor
    func activitySheet(
    	_ item: Binding<ActivityItem?>,
    	permittedArrowDirections: UIPopoverArrowDirection = .any,
    	onComplete: UIActivityViewController.CompletionWithItemsHandler? = nil
    ) -> some View

    Parameters

    item

    The item to use for this activity

    onComplete

    When the sheet is dismissed, the this will be called with the result

    Return Value

    A view.