Advertisement
Guest User

Untitled

a guest
Jul 5th, 2019
831
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.95 KB | None | 0 0
  1. import SwiftUI
  2. import Combine
  3.  
  4. struct AdaptsToSoftwareKeyboard: ViewModifier {
  5. @State var currentHeight: CGFloat = 0
  6.  
  7. func body(content: Content) -> some View {
  8. content
  9. .padding(.bottom, currentHeight)
  10. .edgesIgnoringSafeArea(.bottom)
  11. .onAppear(perform: subscribeToKeyboardEvents)
  12. }
  13.  
  14. private func subscribeToKeyboardEvents() {
  15. NotificationCenter.Publisher(
  16. center: NotificationCenter.default,
  17. name: UIResponder.keyboardWillShowNotification
  18. ).compactMap { notification in
  19. notification.userInfo?["UIKeyboardFrameEndUserInfoKey"] as? CGRect
  20. }.map { rect in
  21. rect.height
  22. }.subscribe(Subscribers.Assign(object: self, keyPath: \.currentHeight))
  23.  
  24. NotificationCenter.Publisher(
  25. center: NotificationCenter.default,
  26. name: UIResponder.keyboardWillHideNotification
  27. ).compactMap { notification in
  28. CGFloat.zero
  29. }.subscribe(Subscribers.Assign(object: self, keyPath: \.currentHeight))
  30. }
  31. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement