-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #10 from ryanlintott/dev
KeyboardHeight
- Loading branch information
Showing
3 changed files
with
116 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
79 changes: 79 additions & 0 deletions
79
Sources/FrameUp/FrameAdjustment/Readers/KeyboardHeightEnvironmentValue.swift
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,79 @@ | ||
// | ||
// KeyboardHeightEnvironmentValue.swift | ||
// KeyboardAvoidance | ||
// | ||
// Created by Ryan Lintott on 2024-02-01. | ||
// | ||
|
||
import SwiftUI | ||
|
||
|
||
private struct KeyboardHeightEnvironmentKey: EnvironmentKey { | ||
static let defaultValue: CGFloat = 0 | ||
} | ||
|
||
public extension EnvironmentValues { | ||
/// Height of software keyboard when visible | ||
var keyboardHeight: CGFloat { | ||
get { self[KeyboardHeightEnvironmentKey.self] } | ||
set { self[KeyboardHeightEnvironmentKey.self] = newValue } | ||
} | ||
} | ||
|
||
public extension Animation { | ||
/// An approximation of Apple's keyboard animation | ||
/// | ||
/// source: https://forums.developer.apple.com/forums/thread/48088 | ||
static var keyboard: Self { | ||
.interpolatingSpring(mass: 3, stiffness: 1000, damping: 500, initialVelocity: 0) | ||
} | ||
} | ||
|
||
#if os(iOS) | ||
struct KeyboardHeightEnvironmentValue: ViewModifier { | ||
@State private var keyboardHeight: CGFloat = 0 | ||
|
||
func body(content: Content) -> some View { | ||
content | ||
.environment(\.keyboardHeight, keyboardHeight) | ||
.animation(.keyboard, value: keyboardHeight) | ||
.background( | ||
GeometryReader { keyboardProxy in | ||
GeometryReader { proxy in | ||
Color.clear | ||
.onChange(of: keyboardProxy.safeAreaInsets.bottom - proxy.safeAreaInsets.bottom) { newValue in | ||
DispatchQueue.main.async { | ||
if keyboardHeight != newValue { | ||
keyboardHeight = newValue | ||
} | ||
} | ||
} | ||
} | ||
.ignoresSafeArea(.keyboard) | ||
} | ||
) | ||
} | ||
} | ||
#endif | ||
|
||
public extension View { | ||
/// Adds an environment value for software keyboard height when visible | ||
/// | ||
/// Must be applied on a view taller than the keyboard that touches the bottom edge of the safe area. | ||
/// Access keyboard height in any child view with | ||
/// @Environment(\.keyboardHeight) var keyboardHeight | ||
func keyboardHeightEnvironmentValue() -> some View { | ||
#if os(iOS) | ||
modifier(KeyboardHeightEnvironmentValue()) | ||
#else | ||
environment(\.keyboardHeight, 0) | ||
#endif | ||
} | ||
} | ||
|
||
#Preview { | ||
VStack { | ||
TextField("Example", text: .constant("")) | ||
} | ||
.keyboardHeightEnvironmentValue() | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters