1

I'm very new to RxSwift and RxCocoa. I want to set image to UIButton by using RxCocoa.

 settingButton.rx.image(for: .normal).onNext(UIImage.init(named: "closeButton"))

Any one have idea how to set image to UIButton? Am i doing right?

Yalamandarao
  • 3,852
  • 3
  • 20
  • 27

2 Answers2

3

I don't recommend you to set a button's image reactively. If you don't have any special reasons for doing this, do it the normal (imperative) way:

settingButton.setImage(UIImage.init(named: "closeButton"), for: .normal)

Here's how you do it reactively, no need for asObserver and stuff:

button.rx.image().onNext(UIImage.init(named: "closeButton"))
Sweeper
  • 213,210
  • 22
  • 193
  • 313
0

Another way:

viewModel.image
   .bind(to: button.rx.image(for: .normal))
   .disposed(by: disposeBag)
Michal Gumny
  • 1,770
  • 1
  • 16
  • 24