Skip to content

Instantly share code, notes, and snippets.

@michzio
Created May 4, 2020 17:06
Show Gist options
  • Save michzio/07d7ee668cafb028b8bf58b1ea2f23df to your computer and use it in GitHub Desktop.
Save michzio/07d7ee668cafb028b8bf58b1ea2f23df to your computer and use it in GitHub Desktop.
struct KeyboardAvoiding<Content>: View where Content: View{
@ObservedObject private var avoider : KeyboardAvoider
let content: () -> Content
let offset: CGFloat
init(with avoider: KeyboardAvoider, offset: CGFloat = 0, @ViewBuilder content: @escaping () -> Content) {
self.content = content
self.avoider = avoider
self.offset = offset
}
var body: some View {
let isKeyboard = avoider.slideSize.height != 0
return self.content()
.offset(y: isKeyboard ? avoider.slideSize.height + offset : 0)
.animation( avoider.isInitialized ? .easeInOut(duration: 1.0) : .none)
.registerKeyboardAvoider(avoider)
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment