exyte / AnimatedTabBar

A tabbar with a number of preset animations written in pure SwiftUI
https://exyte.com/
MIT License
380 stars 26 forks source link

HowTo: Hide `AnimatedTabBar` while Keyboard is visible #14

Closed amirsaam closed 3 months ago

amirsaam commented 3 months ago

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

f3dm76 commented 3 months ago

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

amirsaam commented 3 months ago

@f3dm76 then I think mention it in readme.

f3dm76 commented 3 months ago

It is a standard behaviour