exyte/AnimatedTabBar

HowTo: Hide `AnimatedTabBar` while Keyboard is visible

Closed this issue · 3 comments

While I can add

import SwiftUI
import Combine

private struct KeyboardVisibility: ViewModifier {
#if os(macOS)
  fileprivate func body(content: Content) -> some View {
    content
      .environment(\.keyboardShowing, false)
  }
#else
  @State var isKeyboardShowing: Bool = false
  private var keyboardPublisher: AnyPublisher<Bool, Never> {
    Publishers
      .Merge(
        NotificationCenter
          .default
          .publisher(for: UIResponder.keyboardWillShowNotification)
          .map { _ in true },
        NotificationCenter
          .default
          .publisher(for: UIResponder.keyboardWillHideNotification)
          .map { _ in false })
      .debounce(for: .seconds(0.1), scheduler: RunLoop.main)
      .eraseToAnyPublisher()
  }
  fileprivate func body(content: Content) -> some View {
    content
      .environment(\.keyboardShowing, isKeyboardShowing)
      .onReceive(keyboardPublisher) { value in
        isKeyboardShowing = value
      }
  }
#endif
}

public extension View {
  /// Sets an environment value for keyboardShowing
  /// Access this in any child view with
  /// @Environment(\.keyboardShowing) var keyboardShowing
  func addKeyboardVisibilityToEnvironment() -> some View {
    modifier(KeyboardVisibility())
  }
}

private struct KeyboardShowingEnvironmentKey: EnvironmentKey {
  static let defaultValue: Bool = false
}

extension EnvironmentValues {
  var keyboardShowing: Bool {
    get { self[KeyboardShowingEnvironmentKey.self] }
    set { self[KeyboardShowingEnvironmentKey.self] = newValue }
  }
}

(source) into my app and move AnimatedTabBar into an if closure, but it is something that needs to be handled on the package itself.
IMG_4226

Generally setting up keyboard avoidance should be done on your end, because it is defined for a parent view

@f3dm76 then I think mention it in readme.

It is a standard behaviour