ScrollDismissesKeyboardModifier.swift 1.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142
  1. extension View {
  2. /// Configures the behavior in which scrollable content interacts with the
  3. /// software keyboard.
  4. ///
  5. /// You use this modifier to customize how scrollable content interacts with
  6. /// the software keyboard. For example, you can specify a value of
  7. /// ``ScrollDismissesKeyboardMode/immediately`` to indicate that you would
  8. /// like scrollable content to immediately dismiss the keyboard if present
  9. /// when a scroll drag gesture begins.
  10. ///
  11. /// @State private var text = ""
  12. ///
  13. /// ScrollView {
  14. /// TextField("Prompt", text: $text)
  15. /// ForEach(0 ..< 50) { index in
  16. /// Text("\(index)")
  17. /// .padding()
  18. /// }
  19. /// }
  20. /// .scrollDismissesKeyboard(.immediately)
  21. ///
  22. /// You can also use this modifier to customize the keyboard dismissal
  23. /// behavior for other kinds of scrollable views, like a ``List`` or a
  24. /// ``TextEditor``.
  25. ///
  26. /// By default, scrollable content dismisses the keyboard interactively as
  27. /// the user scrolls. Pass a different value of
  28. /// ``ScrollDismissesKeyboardMode`` to change this behavior. For example,
  29. /// use ``ScrollDismissesKeyboardMode/never`` to prevent the keyboard from
  30. /// dismissing automatically. Note that ``TextEditor`` may still use a
  31. /// different default to preserve expected editing behavior.
  32. ///
  33. /// - Parameter mode: The keyboard dismissal mode that scrollable content
  34. /// uses.
  35. ///
  36. /// - Returns: A view that uses the specified keyboard dismissal mode.
  37. public func scrollDismissesKeyboard(_ mode: ScrollDismissesKeyboardMode) -> some View {
  38. EnvironmentModifier(self) { environment in
  39. environment.with(\.scrollDismissesKeyboardMode, mode)
  40. }
  41. }
  42. }