RxSwiftCommunity / RxKeyboard

Reactive Keyboard in iOS
MIT License
1.6k stars 175 forks source link

`scrollView.contentInset` is reverted back after changing it in visibleHeight callback #94

Open M0rtyMerr opened 5 years ago

M0rtyMerr commented 5 years ago

A simple instance of UIScrollView with UITextField in contentView. I can attach a sample project if it's needed.

RxKeyboard.instance.visibleHeight
   .debug("RxKeyboard")
   .drive(onNext: { [scrollView] in scrollView?.contentInset.bottom = $0 })
   .disposed(by: disposeBag)

scrollView.rx.observe(UIEdgeInsets.self, "contentInset")
  .debug("ScrollView")
  .subscribe()
  .disposed(by: disposeBag)

After assigning zero (keyboard hides) it reverts back to previous value. Output:

2019-07-16 13:16:14.546: RxKeyboard -> Event next(0.0)
2019-07-16 13:16:14.547: ScrollView -> Event next(Optional(__C.UIEdgeInsets(top: 0.0, left: 0.0, bottom: 0.0, right: 0.0)))
2019-07-16 13:16:14.548: ScrollView -> Event next(Optional(__C.UIEdgeInsets(top: 0.0, left: 0.0, bottom: 315.0, right: 0.0)))

Maybe, it's not RxKeyboard problem, however implementing it with raw NotificationCenter resolves the issue. Changing RxKeyboardsubscription code to async (code smell) also resolves the issue:

RxKeyboard.instance.visibleHeight
    .asObservable()
    .observeOn(MainScheduler.asyncInstance)
    .bind { [scrollView] in scrollView?.contentInset.bottom = $0 }
    .disposed(by: disposeBag)

Maybe I just don't understand something. Could u please share your thoughts?