LayoutSubelement

public struct LayoutSubelement

A proxy that represents one child element of a layout.

This type acts as a proxy for a child element in a Layout. Layout protocol methods receive a LayoutSubelements collection that contains exactly one proxy for each of the child elements managed by the layout.

Use this proxy to get information about the associated element, like its size and traits. You should also use the proxy to tell its corresponding element where to appear by calling the proxy’s place(at:anchor:size:) method. Do this once for each subview from your implementation of the layout’s placeSubelements(in:subelements:environment:cache:) method.

Note

The LayoutSubelement API, and its documentation, are modeled after SwiftUI’s LayoutSubview, with major differences noted.
  • Optional attributes to apply to this subelement, such as opacity and transforms.

    Declaration

    Swift

    @LayoutSubelement
    .Storage public var attributes: LayoutSubelement.Attributes { get nonmutating set }
  • Assigns a position and size to a subelement.

    Call this method from your implementation of the Layout protocol’s placeSubelements(in:subelements:environment:cache:) method for each subelement arranged by the layout. Provide a position within the container’s bounds where the subelement should appear, an anchor that indicates which part of the subelement appears at that point, and a size.

    To learn the subelement’s preferred size for a given proposal before calling this method, you can call sizeThatFits(_:) method on the subelement.

    If you call this method more than once for a subelement, the last call takes precedence. If you don’t call this method for a subelement, the subelement fills the bounds of its container.

    Declaration

    Swift

    public func place(
        at position: CGPoint,
        anchor: UnitPoint = .topLeading,
        size: CGSize
    )

    Parameters

    position

    The place where the anchor of the subelement should appear in its container, relative to the container’s bounds.

    anchor

    The unit point on the subelement that appears at position. You can use a built-in point, like center, or you can create a custom UnitPoint.

    size

    The size of the subelement. In Blueprint, parents choose their children’s size. You can determine a good size for a subelement by calling sizeThatFits(_:) on it.

  • Assigns a position and size to a subelement.

    This is a convenience for calling place(at:anchor:size:) with frame.origin and frame.size.

    Declaration

    Swift

    public func place(
        in frame: CGRect,
        anchor: UnitPoint = .topLeading
    )

    Parameters

    frame

    The position and size of the subelement. The origin of this frame represents the place where the anchor of the subelement should appear in its container, relative to the container’s bounds. In Blueprint, parents choose their children’s size. You can determine a good size for a subelement by calling sizeThatFits(_:) on it.

    anchor

    The unit point on the subelement that appears at position. You can use a built-in point, like center, or you can create a custom UnitPoint.

  • Assigns a position and size to a subelement.

    This is a convenience for calling place(at:anchor:size:) with a position of .zero and this size.

    Declaration

    Swift

    public func place(
        filling size: CGSize
    )
  • Asks the subelement for its size.

    In Blueprint, elements are ultimately sized by their parents, but you can use this method to determine the size that a subelement would prefer.

    Declaration

    Swift

    public func sizeThatFits(_ proposal: SizeConstraint) -> CGSize

    Parameters

    proposal

    A proposed size constraint for the subelement.

    Return Value

    The size that the subelement would choose for itself, given the proposal.

  • Gets the layout traits of the subelement.

    Use this method to access the layout-specific Traits value for this subelement.

    Important

    Only call this method with the type of your Layout. For compatibility with legacy layout, this is the only type of traits supported.

    Declaration

    Swift

    public func traits<LayoutType>(
        forLayoutType layoutType: LayoutType.Type
    ) -> LayoutType.Traits where LayoutType: Layout

    Parameters

    layoutType

    The type of layout, which determines the type of the traits.

    Return Value

    The subelements’s layout traits.

  • Optional additional attributes that can be applied to a subelement.

    See more

    Declaration

    Swift

    public struct Attributes