반응형

키보드 show/hide에 따라서 키보드 높이를 사용해야 할때가 있습니다. 이때 Publisher로 처리 하는 방법은 다음과 같습니다.

var keyboardHeightPublisher: AnyPublisher<CGFloat, Never> {
  Publishers.Merge(
    NotificationCenter.default.publisher(for: UIResponder.keyboardWillShowNotification)
      .compactMap { $0.userInfo?[UIResponder.keyboardFrameEndUserInfoKey] as? CGRect }
      .map { $0.height }
    NotificationCenter.default.publisher(for: UIResponder.keyboardWillHideNotification)
      .map { _ in CGFloat(0) }
  ).eraseToAnyPublisher()
}
반응형
Posted by 까칠코더
,