Bartłomiej Semańczyk
Bartłomiej Semańczyk

Reputation: 61774

How to change isEnabled and alpha properties of UIButton base on validation using RXSwift?

This is what I simply try to achieve using:

let observable = Observable.combineLatest(loginProperty.asObservable(), passwordProperty.asObservable()) { _,_ in
    self.viewModel.isValid
}
observable.bind(to: mainView.loginButton.rx.isEnabled).disposed(by: disposeBag)
//observable.bind(to: mainView.loginButton.rx.alpha).disposed(by: disposeBag)

But this can be bound only to isEnabled property. I would like to also change alpha of my loginButton. How can I do that?

Upvotes: 3

Views: 1721

Answers (1)

Jaydeep Vora
Jaydeep Vora

Reputation: 6213

You have to write Reactive extension to bind the property.

extension Reactive where Base : UIButton {
    public var valid : Binder<Bool> {
        return Binder(self.base) { button, valid in
            button.isEnabled = valid
            button.alpha = 0.5 // You Value as per valid or not
        }
    }
}

Then write like this to check the Observable value

observable.bind(to: mainView.loginButton.rx.valid).disposed(by: disposeBag)

Upvotes: 10

Related Questions