Skip to content

#188: Add support to swiping to dismiss keyboard for UIKitBackend #193

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 4 commits into from
Jun 27, 2025
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions Examples/Sources/NotesExample/ContentView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -142,6 +142,7 @@ struct ContentView: View {
.padding()
.background(textEditorBackground)
.cornerRadius(4)
.scrollDismissesKeyboard(.interactively)
}
}
.padding()
Expand Down
4 changes: 4 additions & 0 deletions Sources/AppKitBackend/AppKitBackend.swift
Original file line number Diff line number Diff line change
Expand Up @@ -861,6 +861,10 @@ public final class AppKitBackend: AppBackend {
return scrollView
}

public func updateScrollContainer(_ scrollView: Widget, environment: EnvironmentValues) {
let scrollView = scrollView as! NSScrollView
}

public func setScrollBarPresence(
ofScrollContainer scrollView: Widget,
hasVerticalScrollBar: Bool,
Expand Down
2 changes: 2 additions & 0 deletions Sources/CursesBackend/CursesBackend.swift
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,8 @@ public final class CursesBackend: AppBackend {

public func setSpacing(ofHStack container: Widget, to spacing: Int) {}

public func updateScrollContainer(_ scrollView: Widget, environment: EnvironmentValues) {}

public func createTextView() -> Widget {
let label = Label("")
label.width = Dim.fill()
Expand Down
4 changes: 4 additions & 0 deletions Sources/Gtk3Backend/Gtk3Backend.swift
Original file line number Diff line number Diff line change
Expand Up @@ -539,6 +539,10 @@ public final class Gtk3Backend: AppBackend {
return scrollView
}

public func updateScrollContainer(_ scrollView: Widget, environment: EnvironmentValues) {
let scrollView = scrollView as! ScrolledWindow
}

public func setScrollBarPresence(
ofScrollContainer scrollView: Widget,
hasVerticalScrollBar: Bool,
Expand Down
4 changes: 4 additions & 0 deletions Sources/GtkBackend/GtkBackend.swift
Original file line number Diff line number Diff line change
Expand Up @@ -503,6 +503,10 @@ public final class GtkBackend: AppBackend {
return scrollView
}

public func updateScrollContainer(_ scrollView: Widget, environment: EnvironmentValues) {
let scrollView = scrollView as! ScrolledWindow
}

public func setScrollBarPresence(
ofScrollContainer scrollView: Widget,
hasVerticalScrollBar: Bool,
Expand Down
2 changes: 2 additions & 0 deletions Sources/LVGLBackend/LVGLBackend.swift
Original file line number Diff line number Diff line change
Expand Up @@ -156,6 +156,8 @@ public final class LVGLBackend: AppBackend {
}
}

public func updateScrollContainer(_ scrollView: Widget, environment: EnvironmentValues) {}

public func createTextView() -> Widget {
return Widget { parent in
let label = LVLabel(with: parent)
Expand Down
2 changes: 2 additions & 0 deletions Sources/QtBackend/QtBackend.swift
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,8 @@ public struct QtBackend: AppBackend {
}
}

public func updateScrollContainer(_ scrollView: Widget, environment: EnvironmentValues) {}

public func setSpacing(ofHStack widget: Widget, to spacing: Int) {
(widget.layout as! QHBoxLayout).spacing = Int32(spacing)
}
Expand Down
18 changes: 18 additions & 0 deletions Sources/SwiftCrossUI/Backend/AppBackend.swift
Original file line number Diff line number Diff line change
Expand Up @@ -264,6 +264,20 @@ public protocol AppBackend {

/// Creates a scrollable single-child container wrapping the given widget.
func createScrollContainer(for child: Widget) -> Widget
/// Updates a scroll container with environment-specific values.
///
/// This method is primarily used on iOS to apply environment changes
/// that affect the scroll view’s behavior, such as keyboard dismissal mode.
/// It allows the backend to update UIKit-specific properties (e.g. `keyboardDismissMode`)
/// when the environment changes.
///
/// - Parameters:
/// - scrollView: The scroll container widget previously created by `createScrollContainer(for:)`.
/// - environment: The current `EnvironmentValues` to apply.
func updateScrollContainer(
_ scrollView: Widget,
environment: EnvironmentValues
)
/// Sets the presence of scroll bars along each axis of a scroll container.
func setScrollBarPresence(
ofScrollContainer scrollView: Widget,
Expand Down Expand Up @@ -741,6 +755,10 @@ extension AppBackend {
todo()
}

public func updateScrollContainer(_ scrollView: Widget, environment: EnvironmentValues) {
todo()
}

public func setScrollBarPresence(
ofScrollContainer scrollView: Widget,
hasVerticalScrollBar: Bool,
Expand Down
3 changes: 3 additions & 0 deletions Sources/SwiftCrossUI/Environment/EnvironmentValues.swift
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,8 @@ public struct EnvironmentValues {

/// Whether user interaction is enabled. Set by ``View/disabled(_:)``.
public var isEnabled: Bool

public var scrollDismissesKeyboardMode: ScrollDismissesKeyboardMode

/// Called by view graph nodes when they resize due to an internal state
/// change and end up changing size. Each view graph node sets its own
Expand Down Expand Up @@ -196,6 +198,7 @@ public struct EnvironmentValues {
listStyle = .default
toggleStyle = .button
isEnabled = true
scrollDismissesKeyboardMode = .interactively
}

/// Returns a copy of the environment with the specified property set to the
Expand Down
20 changes: 20 additions & 0 deletions Sources/SwiftCrossUI/Values/ScrollDismissesKeyboardMode.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
/// The ways that scrollable content can interact with the software keyboard.
///
/// Use this type in a call to the ``View/scrollDismissesKeyboard(_:)``
/// modifier to specify the dismissal behavior of scrollable views.
public enum ScrollDismissesKeyboardMode: Sendable {
/// Dismiss the keyboard as soon as scrolling starts.
case immediately

/// Enable people to interactively dismiss the keyboard as part of the
/// scroll operation.
///
/// The software keyboard's position tracks the gesture that drives the
/// scroll operation if the gesture crosses into the keyboard's area of the
/// display. People can dismiss the keyboard by scrolling it off the
/// display, or reverse the direction of the scroll to cancel the dismissal.
case interactively

/// Never dismiss the keyboard automatically as a result of scrolling.
case never
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
extension View {
/// Configures the behavior in which scrollable content interacts with
/// the software keyboard.
///
/// You use this modifier to customize how scrollable content interacts
/// with the software keyboard. For example, you can specify a value of
/// ``ScrollDismissesKeyboardMode/immediately`` to indicate that you
/// would like scrollable content to immediately dismiss the keyboard if
/// present when a scroll drag gesture begins.
///
/// @State private var text = ""
///
/// ScrollView {
/// TextField("Prompt", text: $text)
/// ForEach(0 ..< 50) { index in
/// Text("\(index)")
/// .padding()
/// }
/// }
/// .scrollDismissesKeyboard(.immediately)
///
/// You can also use this modifier to customize the keyboard dismissal
/// behavior for other kinds of scrollable views, like a ``List`` or a
/// ``TextEditor``.
///
/// By default, scrollable content dismisses the keyboard interactively as the user scrolls.
/// Pass a different value of ``ScrollDismissesKeyboardMode`` to change this behavior.
/// For example, use ``ScrollDismissesKeyboardMode/never`` to prevent the keyboard from
/// dismissing automatically. Note that ``TextEditor`` may still use a different
/// default to preserve expected editing behavior.
///
/// - Parameter mode: The keyboard dismissal mode that scrollable content
/// uses.
///
/// - Returns: A view that uses the specified keyboard dismissal mode.
public func scrollDismissesKeyboard(_ mode: ScrollDismissesKeyboardMode) -> some View {
EnvironmentModifier(self) { environment in
environment.with(\.scrollDismissesKeyboardMode, mode)
}
}
}
1 change: 1 addition & 0 deletions Sources/SwiftCrossUI/Views/ScrollView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -143,6 +143,7 @@ public struct ScrollView<Content: View>: TypeSafeView, View {
hasVerticalScrollBar: hasVerticalScrollBar,
hasHorizontalScrollBar: hasHorizontalScrollBar
)
backend.updateScrollContainer(widget, environment: environment)
} else {
finalResult = childResult
}
Expand Down
18 changes: 18 additions & 0 deletions Sources/UIKitBackend/UIKitBackend+Container.swift
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,19 @@ final class ScrollWidget: ContainerWidget {
scrollView.showsVerticalScrollIndicator = hasVerticalScrollBar
scrollView.showsHorizontalScrollIndicator = hasHorizontalScrollBar
}

public func updateScrollContainer(environment: EnvironmentValues) {
#if os(iOS)
scrollView.keyboardDismissMode = switch environment.scrollDismissesKeyboardMode {
case .immediately:
.onDrag
case .interactively:
.interactive
case .never:
.none
}
#endif
}
}

extension UIKitBackend {
Expand Down Expand Up @@ -121,6 +134,11 @@ extension UIKitBackend {
ScrollWidget(child: child)
}

public func updateScrollContainer(_ scrollView: Widget, environment: EnvironmentValues) {
let scrollViewWidget = scrollView as! ScrollWidget
scrollViewWidget.updateScrollContainer(environment: environment)
}

public func setScrollBarPresence(
ofScrollContainer scrollView: Widget,
hasVerticalScrollBar: Bool,
Expand Down
10 changes: 10 additions & 0 deletions Sources/UIKitBackend/UIKitBackend+Control.swift
Original file line number Diff line number Diff line change
Expand Up @@ -317,6 +317,16 @@ extension UIKitBackend {
} else {
textEditorWidget.child.inputAccessoryView = nil
}

textEditorWidget.child.alwaysBounceVertical = environment.scrollDismissesKeyboardMode != .never
textEditorWidget.child.keyboardDismissMode = switch environment.scrollDismissesKeyboardMode {
case .immediately:
textEditorWidget.child.inputAccessoryView == nil ? .onDrag : .onDragWithAccessory
case .interactively:
textEditorWidget.child.inputAccessoryView == nil ? .interactive : .interactiveWithAccessory
case .never:
.none
}
#endif
}

Expand Down
4 changes: 4 additions & 0 deletions Sources/WinUIBackend/WinUIBackend.swift
Original file line number Diff line number Diff line change
Expand Up @@ -616,6 +616,10 @@ public final class WinUIBackend: AppBackend {
return scrollViewer
}

public func updateScrollContainer(_ scrollView: Widget, environment: EnvironmentValues) {
let scrollView = scrollView as! WinUI.ScrollViewer
}

public func setScrollBarPresence(
ofScrollContainer scrollView: Widget,
hasVerticalScrollBar: Bool,
Expand Down