Created
November 25, 2020 18:14
-
-
Save mrmemmo/9ed6c1975cbc434855e66659a87c6c1d to your computer and use it in GitHub Desktop.
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
// | |
// KeyboardHandler.swift | |
// te3st1 | |
// | |
// Created by Episcopal Academy on 11/25/20. | |
// Copyright © 2020 Episcopal Academy. All rights reserved. | |
// | |
import Combine | |
import SwiftUI | |
final class KeyboardHandler: ObservableObject { | |
@Published private(set) var keyboardheight:CGFloat = 0 | |
private var cancellable: AnyCancellable? | |
private let keyboardWillShow = NotificationCenter.default.publisher(for: UIResponder.keyboardWillShowNotification) | |
.compactMap{($0.userInfo?[UIResponder.keyboardFrameEndUserInfoKey] as? CGRect)?.height} | |
private let keyboardWillHide = NotificationCenter.default.publisher(for: UIResponder.keyboardWillHideNotification) | |
.map { _ in CGFloat.zero} | |
init(){ | |
cancellable = Publishers.Merge(keyboardWillShow, keyboardWillHide) | |
.subscribe(on: DispatchQueue.main) | |
.assign(to:\.self.keyboardheight, on: self) | |
} | |
} | |
======================== | |
content view | |
======================= | |
@ObservedObject private var keyboardHandler = KeyboardHandler() | |
//end of VStack | |
.padding(.bottom, keyboardHandler.keyboardheight) | |
.animation(.easeIn) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment