옵저버를 추가하고 해제하는 작업이 자동으로 처리됨
여기서는 키보드 표시에 따른 텍스트뷰 조정 처리 작업
let willShowObservable = NotificationCenter.default.rx.notification(UIResponder.keyboardWillShowNotification)
.map { ($0.userInfo?[UIResponder.keyboardFrameEndUserInfoKey] as? NSValue)?.cgRectValue.height ?? 0 }
let willHideObservable = NotificationCenter.default.rx.notification(UIResponder.keyboardWillHideNotification)
.map { noti -> CGFloat in 0 }
Observable.merge(willShowObservable, willHideObservable)
.map { [unowned self] height -> UIEdgeInsets in
var inset = self.textView.contentInset
inset.bottom = height
return inset
}
.subscribe(onNext: { [weak self] inset in
UIView.animate(withDuration: 0.3) {
self?.textView.contentInset = inset
}
})
.disposed(by: bag)