Skip to content

Instantly share code, notes, and snippets.

@mrmemmo
Created November 25, 2020 18:14
Show Gist options
  • Save mrmemmo/9ed6c1975cbc434855e66659a87c6c1d to your computer and use it in GitHub Desktop.
Save mrmemmo/9ed6c1975cbc434855e66659a87c6c1d to your computer and use it in GitHub Desktop.
//
// 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