Skip to content

Instantly share code, notes, and snippets.

@slangley
Created September 25, 2015 19:55
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save slangley/bfeede1cc368f6d14260 to your computer and use it in GitHub Desktop.
Save slangley/bfeede1cc368f6d14260 to your computer and use it in GitHub Desktop.
RxSwift UIButton binding for setImage:controlState:
import UIKit
import RxSwift
import RxCocoa
extension UIButton {
public func rx_image(controlState: UIControlState?) -> ObserverOf<UIImage!> {
return ObserverOf { [weak self] event in
MainScheduler.ensureExecutingOnScheduler()
switch event {
case .Next(let value):
if let controlState = controlState {
self?.setImage( value, forState:controlState)
}
else {
self?.setImage( value, forState:.Normal)
}
case .Error(let error):
let error = "Binding error to UI: \(error)"
print(error)
case .Completed:
break
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment