Last active
April 19, 2024 12:56
-
-
Save scottmatthewman/722987c9ad40f852e2b6a185f390f88d to your computer and use it in GitHub Desktop.
An example of using Combine to automatically adapt a SwiftUI scrollable view to accommodate an iOS onscreen keyboard
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
import SwiftUI | |
import Combine | |
struct AdaptsToSoftwareKeyboard: ViewModifier { | |
@State var currentHeight: CGFloat = 0 | |
func body(content: Content) -> some View { | |
content | |
.padding(.bottom, currentHeight) | |
.edgesIgnoringSafeArea(.bottom) | |
.onAppear(perform: subscribeToKeyboardEvents) | |
} | |
private func subscribeToKeyboardEvents() { | |
NotificationCenter.Publisher( | |
center: NotificationCenter.default, | |
name: UIResponder.keyboardWillShowNotification | |
).compactMap { notification in | |
notification.userInfo?["UIKeyboardFrameEndUserInfoKey"] as? CGRect | |
}.map { rect in | |
rect.height | |
}.subscribe(Subscribers.Assign(object: self, keyPath: \.currentHeight)) | |
NotificationCenter.Publisher( | |
center: NotificationCenter.default, | |
name: UIResponder.keyboardWillHideNotification | |
).compactMap { notification in | |
CGFloat.zero | |
}.subscribe(Subscribers.Assign(object: self, keyPath: \.currentHeight)) | |
} | |
} |
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
import SwiftUI | |
struct KeyboardAwareScrollableView : View { | |
@ObjectBinding var venue: SomeDataModel | |
var body: some View { | |
// Apply to any view with an instrinsic scroll view – ScrollView, Form, List, etc | |
Form { | |
Section { | |
TextField($venue.name, placeholder: Text("Venue Name")) | |
} | |
Section(header: Text("Address")) { | |
TextField($venue.street, placeholder: Text("Street")) | |
TextField($venue.city, placeholder: Text("City")) | |
TextField($venue.country, placeholder: Text("Country")) | |
TextField($venue.postalCode, placeholder: Text("Postcode/ZIP")) | |
} | |
// etc. | |
} | |
.navigationBarTitle(Text("New venue")) | |
.modifier(AdaptsToSoftwareKeyboard()) // <-- apply the modifier here | |
} | |
} |
@jaimeealmanza to go a step further, you can use the following animation to match the keyboard transition more closely:
extension Animation {
static var keyboard: Animation {
.interpolatingSpring(mass: 3, stiffness: 1000, damping: 500, initialVelocity: 0.0)
}
}
Did you notice that this code stopped working with Xcode 12 beta 3 on iOS 14 beta 3? At least for me, it works great on beta 2 and earlier. The notification process seems to work fine but the view doesn't seem to respect the padding. Can't quite figure out why that is. Thanks!
@ntornado see this tweet.
@ntornado see this tweet.
Thanks, @damirstuhec!
It doesn't work for me unfortunately. I only got ScrollView with 10 textfields. I think scrollview don't respect padding in Xcode 12.5.1
Nice :)
Will this work for TextEditor?
@ethanyuwang did you find a solution for your issue?
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
This works perfectly!
You could also add
.animation(.default)
after.padding(.bottom, currentHeight)
to smooth out the transition since it's pretty fast and it's somewhat noticeable.