-
-
Save perlguy99/4f74c3ee7073921123c0df151ae7fe51 to your computer and use it in GitHub Desktop.
// | |
// KeyboardAwareModifier.swift | |
// KeyboardTest | |
// | |
import SwiftUI | |
import Combine | |
struct KeyboardAwareModifier: ViewModifier { | |
@State private var keyboardHeight: CGFloat = 0 | |
private var keyboardHeightPublisher: AnyPublisher<CGFloat, Never> { | |
Publishers.Merge( | |
NotificationCenter.default | |
.publisher(for: UIResponder.keyboardWillShowNotification) | |
.compactMap { $0.userInfo?[UIResponder.keyboardFrameEndUserInfoKey] as? CGRect } | |
.map { $0.height }, | |
NotificationCenter.default | |
.publisher(for: UIResponder.keyboardWillHideNotification) | |
.map { _ in CGFloat(0) } | |
).eraseToAnyPublisher() | |
} | |
func body(content: Content) -> some View { | |
content | |
.padding(.bottom, keyboardHeight) | |
.onReceive(keyboardHeightPublisher) { self.keyboardHeight = $0 } | |
} | |
} | |
extension View { | |
func keyboardAwarePadding() -> some View { | |
ModifiedContent(content: self, modifier: KeyboardAwareModifier()) | |
} | |
} |
Really? You are serious?
I probably have several Gists that are copied from Stack Overflow (public domain) which in reality, I have NO idea who first wrote the code.
I really only create Gists for my personal use - I will just start making all of my Gists private so nobody has to take the time to try and tell me that someone else may have written it first.
Between making your gists private and not providing attribution, I prefer the latter. The gist is useful. I wouldn't say anything if this was a verbatim copy of an answer from SO. Attribution would be cool, but that never happens, I know that. But adding your own copyright line? That's just unnecessary, no? Anyway, I didn't mean to discourage you from making your stuff public, I merely meant to encourage you to add attribution and remove invalid copyright line.
My own copyright line came from Xcode. I'll remove it.
A different person put together this code for a Stack Overflow question, seemingly before you did:
https://stackoverflow.com/a/59098816/2715716
Unless you are the original author of this snippet (can't be sure the other person is either), you should probably cool it with the copyright and the missing attribution.