|
| 1 | +// |
| 2 | +// Spacer.swift |
| 3 | +// OpenSwiftUI |
| 4 | +// |
| 5 | +// Audited for RELEASE_2021 |
| 6 | +// Status: TODO |
| 7 | + |
| 8 | +import Foundation |
| 9 | + |
| 10 | +/// A flexible space that expands along the major axis of its containing stack |
| 11 | +/// layout, or on both axes if not contained in a stack. |
| 12 | +/// |
| 13 | +/// A spacer creates an adaptive view with no content that expands as much as |
| 14 | +/// it can. For example, when placed within an ``HStack``, a spacer expands |
| 15 | +/// horizontally as much as the stack allows, moving sibling views out of the |
| 16 | +/// way, within the limits of the stack's size. |
| 17 | +/// OpenSwiftUI sizes a stack that doesn't contain a spacer up to the combined |
| 18 | +/// ideal widths of the content of the stack's child views. |
| 19 | +/// |
| 20 | +/// The following example provides a simple checklist row to illustrate how you |
| 21 | +/// can use a spacer: |
| 22 | +/// |
| 23 | +/// struct ChecklistRow: View { |
| 24 | +/// let name: String |
| 25 | +/// |
| 26 | +/// var body: some View { |
| 27 | +/// HStack { |
| 28 | +/// Image(systemName: "checkmark") |
| 29 | +/// Text(name) |
| 30 | +/// } |
| 31 | +/// .border(Color.blue) |
| 32 | +/// } |
| 33 | +/// } |
| 34 | +/// |
| 35 | +///  |
| 39 | +/// |
| 40 | +/// Adding a spacer before the image creates an adaptive view with no content |
| 41 | +/// that expands to push the image and text to the right side of the stack. |
| 42 | +/// The stack also now expands to take as much space as the parent view allows, |
| 43 | +/// shown by the blue border that indicates the boundary of the stack: |
| 44 | +/// |
| 45 | +/// struct ChecklistRow: View { |
| 46 | +/// let name: String |
| 47 | +/// |
| 48 | +/// var body: some View { |
| 49 | +/// HStack { |
| 50 | +/// Spacer() |
| 51 | +/// Image(systemName: "checkmark") |
| 52 | +/// Text(name) |
| 53 | +/// } |
| 54 | +/// .border(Color.blue) |
| 55 | +/// } |
| 56 | +/// } |
| 57 | +/// |
| 58 | +///  |
| 62 | +/// |
| 63 | +/// Moving the spacer between the image and the name pushes those elements to |
| 64 | +/// the left and right sides of the ``HStack``, respectively. Because the stack |
| 65 | +/// contains the spacer, it expands to take as much horizontal space as the |
| 66 | +/// parent view allows; the blue border indicates its size: |
| 67 | +/// |
| 68 | +/// struct ChecklistRow: View { |
| 69 | +/// let name: String |
| 70 | +/// |
| 71 | +/// var body: some View { |
| 72 | +/// HStack { |
| 73 | +/// Image(systemName: "checkmark") |
| 74 | +/// Spacer() |
| 75 | +/// Text(name) |
| 76 | +/// } |
| 77 | +/// .border(Color.blue) |
| 78 | +/// } |
| 79 | +/// } |
| 80 | +/// |
| 81 | +///  |
| 86 | +/// |
| 87 | +/// Adding two spacer views on the outside of the stack leaves the image and |
| 88 | +/// text together, while the stack expands to take as much horizontal space |
| 89 | +/// as the parent view allows: |
| 90 | +/// |
| 91 | +/// struct ChecklistRow: View { |
| 92 | +/// let name: String |
| 93 | +/// |
| 94 | +/// var body: some View { |
| 95 | +/// HStack { |
| 96 | +/// Spacer() |
| 97 | +/// Image(systemName: "checkmark") |
| 98 | +/// Text(name) |
| 99 | +/// Spacer() |
| 100 | +/// } |
| 101 | +/// .border(Color.blue) |
| 102 | +/// } |
| 103 | +/// } |
| 104 | +/// |
| 105 | +///  |
| 110 | +@frozen |
| 111 | +public struct Spacer { |
| 112 | + /// The minimum length this spacer can be shrunk to, along the axis or axes |
| 113 | + /// of expansion. |
| 114 | + /// |
| 115 | + /// If `nil`, the system default spacing between views is used. |
| 116 | + public var minLength: CGFloat? |
| 117 | + |
| 118 | + @inlinable |
| 119 | + public init(minLength: CGFloat? = nil) { |
| 120 | + self.minLength = minLength |
| 121 | + } |
| 122 | +} |
| 123 | + |
| 124 | +extension Spacer: PrimitiveView {} |
0 commit comments