Implemented the ResignFocusModifier view modifier in the app target.
This commit is contained in:
@@ -0,0 +1,43 @@
|
|||||||
|
import SwiftUI
|
||||||
|
|
||||||
|
/// A view modifier that resigns the first responder status when the view is tapped.
|
||||||
|
///
|
||||||
|
/// This modifier is useful for dismissing the keyboard or other input controls
|
||||||
|
/// when the user taps on an area of the view that doesn't have interactive content.
|
||||||
|
struct ResignFocusModifier: ViewModifier {
|
||||||
|
|
||||||
|
// MARK: Body
|
||||||
|
|
||||||
|
/// The content and behavior of the view modifier.
|
||||||
|
///
|
||||||
|
/// Adds a tap gesture that sends the `resignFirstResponder` action
|
||||||
|
/// to dismiss any active first responder (such as a text field).
|
||||||
|
func body(content: Content) -> some View {
|
||||||
|
content
|
||||||
|
.onTapGesture {
|
||||||
|
UIApplication.shared.sendAction(
|
||||||
|
#selector(UIResponder.resignFirstResponder),
|
||||||
|
to: nil,
|
||||||
|
from: nil,
|
||||||
|
for: nil
|
||||||
|
)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
// MARK: - View Extension
|
||||||
|
|
||||||
|
extension View {
|
||||||
|
|
||||||
|
/// Adds a tap gesture that resigns the first responder when the view is tapped.
|
||||||
|
///
|
||||||
|
/// This is useful for dismissing the keyboard or other input controls
|
||||||
|
/// when the user taps outside of an interactive element.
|
||||||
|
///
|
||||||
|
/// - Returns: A view that resigns first responder status on tap.
|
||||||
|
func onResignFocus() -> some View {
|
||||||
|
modifier(ResignFocusModifier())
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user