|
12 | 12 |
|
13 | 13 | ## Overview |
14 | 14 |
|
15 | | -🔥🔥🔥 **NEW** SwiftUI support added! |
| 15 | +🔥🔥🔥 **UPDATE** SwiftUI support in in Xcode 26 requries 10.0.2! |
16 | 16 |
|
17 | 17 | SwiftMessages is a very flexible view and view controller presentation library for UIKit and SwiftUI. |
18 | 18 |
|
@@ -181,6 +181,8 @@ And check out our blog post [Elegant Custom UIViewController Transitioning](http |
181 | 181 |
|
182 | 182 | Any of the built-in SwiftMessages views can be displayed by calling the SwiftMessages APIs from within observable object, a button action closure, etc. However, SwiftMessages can also display your custom SwiftUI views. |
183 | 183 |
|
| 184 | +#### Presentation |
| 185 | +
|
184 | 186 | Take the following message view and companion data model: |
185 | 187 |
|
186 | 188 | ````swift |
@@ -276,6 +278,63 @@ struct DemoView: View { |
276 | 278 | } |
277 | 279 | ```` |
278 | 280 |
|
| 281 | +#### Dismissal |
| 282 | +
|
| 283 | +SwiftMessages provides several approaches for dismissing messages from within SwiftUI views. |
| 284 | +
|
| 285 | +For messages shown using the `swiftMessage()` modifier, you can dismiss by setting the binding to `nil`: |
| 286 | +
|
| 287 | +````swift |
| 288 | +struct DemoView: View { |
| 289 | + @State var message: DemoMessage? |
| 290 | +
|
| 291 | + var body: some View { |
| 292 | + VStack { |
| 293 | + Button("Show message") { |
| 294 | + message = DemoMessage(title: "Demo", body: "SwiftUI forever!") |
| 295 | + } |
| 296 | + Button("Hide message") { |
| 297 | + message = nil // Dismisses the message |
| 298 | + } |
| 299 | + } |
| 300 | + .swiftMessage(message: $message) { message in |
| 301 | + DemoMessageView(message: message) |
| 302 | + } |
| 303 | + } |
| 304 | +} |
| 305 | +```` |
| 306 | +
|
| 307 | +For messages that need to dismiss themselves, SwiftMessages provides a SwiftUI-style environment action: |
| 308 | +
|
| 309 | +````swift |
| 310 | +struct SelfDismissingMessageView: View { |
| 311 | + @Environment(\.swiftMessagesHide) private var hide |
| 312 | + let message: DemoMessage |
| 313 | +
|
| 314 | + var body: some View { |
| 315 | + VStack(alignment: .leading) { |
| 316 | + Text(message.title).font(.system(size: 20, weight: .bold)) |
| 317 | + Text(message.body) |
| 318 | + |
| 319 | + Button("Dismiss") { |
| 320 | + hide(animated: true) // Self-dismiss with animation |
| 321 | + } |
| 322 | + } |
| 323 | + .padding(30) |
| 324 | + .frame(maxWidth: .infinity) |
| 325 | + .background(.gray) |
| 326 | + } |
| 327 | +} |
| 328 | +```` |
| 329 | +
|
| 330 | +You can also call the SwiftMessages APIs directly from within your SwiftUI views: |
| 331 | +
|
| 332 | +````swift |
| 333 | +Button("Hide Current") { |
| 334 | + SwiftMessages.hide() |
| 335 | +} |
| 336 | +```` |
| 337 | +
|
279 | 338 | Try it out in the SwiftUI demo app! |
280 | 339 |
|
281 | 340 | ### Accessibility |
|
0 commit comments