Skip to content

Instantly share code, notes, and snippets.

@perlguy99
Last active October 23, 2020 04:22
Show Gist options
  • Save perlguy99/4f74c3ee7073921123c0df151ae7fe51 to your computer and use it in GitHub Desktop.
Save perlguy99/4f74c3ee7073921123c0df151ae7fe51 to your computer and use it in GitHub Desktop.
SwiftUI Keyboard Aware Modifier
//
// 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())
}
}
@perlguy99
Copy link
Author

perlguy99 commented Dec 17, 2019

This modifier is NOT perfect, but it is definitely better than nothing!

Currently, if you tab to the next field, it will only tab to fields that are currently visible on the screen.

  • Adding a constant to the keyboardHeight property doesn't fix the issue, it just creates a larger blank space above the keyboard.

@TomasHubelbauer
Copy link

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.

@perlguy99
Copy link
Author

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.

@TomasHubelbauer
Copy link

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.

@perlguy99
Copy link
Author

My own copyright line came from Xcode. I'll remove it.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment