// // KeyboardHelper.swift // Pi-Helper // // Created by Billy Brawner on 1/23/20. // Copyright © 2020 William Brawner. All rights reserved. // // Adapted from https://stackoverflow.com/a/59098816 import SwiftUI import Combine struct KeyboardHelper: ViewModifier { @State private var keyboardHeight: CGFloat = 0 private var keyboardHeightPublisher: AnyPublisher { Publishers.Merge( NotificationCenter.default .publisher(for: UIResponder.keyboardWillShowNotification) .compactMap { $0.userInfo?[UIResponder.keyboardFrameEndUserInfoKey] as? NSValue } .map { $0.cgRectValue.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: KeyboardHelper()) } }