|
| 1 | +// |
| 2 | +// View+Overlay.swift |
| 3 | +// ShortcutUI |
| 4 | +// |
| 5 | +// Created by Sheikh Bayazid on 2022-12-15. |
| 6 | +// |
| 7 | + |
| 8 | +import SwiftUI |
| 9 | + |
| 10 | +public extension View { |
| 11 | + /// Presents an overlay when a Boolean value that you provide is true. |
| 12 | + /// |
| 13 | + /// Use this method when you want to present an overlay view to the |
| 14 | + /// user when a Boolean value you provide is true. The example |
| 15 | + /// below displays an overlay view when the user toggles the |
| 16 | + /// `isShowingOverlay` variable by clicking or tapping on |
| 17 | + /// the "Show overlay" button: |
| 18 | + /// |
| 19 | + /// struct Example: View { |
| 20 | + /// @State private var isShowingOverlay = false |
| 21 | + /// |
| 22 | + /// var body: some View { |
| 23 | + /// VStack(spacing: 50) { |
| 24 | + /// Button("Show overlay") { |
| 25 | + /// isShowingOverlay.toggle() |
| 26 | + /// } |
| 27 | + /// |
| 28 | + /// RoundedRectangle(cornerRadius: 12) |
| 29 | + /// .fill(Color.blue) |
| 30 | + /// .overlay(isPresented: isShowingOverlay) { |
| 31 | + /// Text("Hello, world!") |
| 32 | + /// } |
| 33 | + /// } |
| 34 | + /// } |
| 35 | + /// } |
| 36 | + /// |
| 37 | + /// - Parameters: |
| 38 | + /// - alignment: The alignment of the overlay content that you create in the modifier's |
| 39 | + /// `content` closure.. The default is `Alignment/center`. |
| 40 | + /// - isPresented: A Boolean value that determines whether |
| 41 | + /// to present the overlay that you create in the modifier's `content` closure. |
| 42 | + /// - content: A closure that returns the content of the overlay. |
| 43 | + @ViewBuilder |
| 44 | + func overlay<Content: View>( |
| 45 | + alignment: Alignment = .center, |
| 46 | + isPresented: Bool, |
| 47 | + @ViewBuilder content: () -> Content |
| 48 | + ) -> some View { |
| 49 | + overlay( |
| 50 | + isPresented ? content() : nil, |
| 51 | + alignment: alignment |
| 52 | + ) |
| 53 | + } |
| 54 | +} |
0 commit comments