Yalamandarao
Yalamandarao

Reputation: 3862

How to set image to UIButton by using RxCocoa

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?

Upvotes: 1

Views: 1466

Answers (2)

Michal Gumny
Michal Gumny

Reputation: 1770

Another way:

viewModel.image
   .bind(to: button.rx.image(for: .normal))
   .disposed(by: disposeBag)

Upvotes: 0

Sweeper
Sweeper

Reputation: 274105

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"))

Upvotes: 3

Related Questions