Skip to content

Commit

Permalink
Add UIBindingObserver for updating UIButton's image
Browse files Browse the repository at this point in the history
  • Loading branch information
crazytonyli authored and kzaher committed May 29, 2017
1 parent a109414 commit b4efef3
Show file tree
Hide file tree
Showing 2 changed files with 68 additions and 0 deletions.
14 changes: 14 additions & 0 deletions RxCocoa/iOS/UIButton+Rx.swift
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,20 @@ extension Reactive where Base: UIButton {
button.setTitle(title, for: controlState)
}
}

/// Reactive wrapper for `setImage(_:for:)`
public func image(for controlState: UIControlState = []) -> UIBindingObserver<Base, UIImage?> {
return UIBindingObserver<Base, UIImage?>(UIElement: self.base) { (button, image) -> () in
button.setImage(image, for: controlState)
}
}

/// Reactive wrapper for `setBackgroundImage(_:for:)`
public func backgroundImage(for controlState: UIControlState = []) -> UIBindingObserver<Base, UIImage?> {
return UIBindingObserver<Base, UIImage?>(UIElement: self.base) { (button, image) -> () in
button.setBackgroundImage(image, for: controlState)
}
}

}
#endif
Expand Down
54 changes: 54 additions & 0 deletions Tests/RxCocoaTests/UIButton+RxTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,60 @@ extension UIButtonTests {
let createView: () -> UIButton = { UIButton(frame: CGRect(x: 0, y: 0, width: 1, height: 1)) }
ensureEventDeallocated(createView) { (view: UIButton) in view.rx.tap }
}

func testImageNormal() {
let button = UIButton(frame: CGRect(x: 0, y: 0, width: 1, height: 1))
let image = UIImage()

XCTAssertFalse(button.image(for: .normal) == image)
_ = Observable.just(image).subscribe(button.rx.image(for: .normal))
XCTAssertTrue(button.image(for: .normal) == image)
}

func testImageSelected() {
let button = UIButton(frame: CGRect(x: 0, y: 0, width: 1, height: 1))
let image = UIImage()

XCTAssertFalse(button.image(for: .selected) == image)
_ = Observable.just(image).subscribe(button.rx.image(for: .selected))
XCTAssertTrue(button.image(for: .selected) == image)
}

func testImageDefault() {
let button = UIButton(frame: CGRect(x: 0, y: 0, width: 1, height: 1))
let image = UIImage()

XCTAssertFalse(button.image(for: []) == image)
_ = Observable.just(image).subscribe(button.rx.image())
XCTAssertTrue(button.image(for: []) == image)
}

func testBackgroundImageNormal() {
let button = UIButton(frame: CGRect(x: 0, y: 0, width: 1, height: 1))
let image = UIImage()

XCTAssertFalse(button.backgroundImage(for: .normal) == image)
_ = Observable.just(image).subscribe(button.rx.backgroundImage(for: .normal))
XCTAssertTrue(button.backgroundImage(for: .normal) == image)
}

func testBackgroundImageSelected() {
let button = UIButton(frame: CGRect(x: 0, y: 0, width: 1, height: 1))
let image = UIImage()

XCTAssertFalse(button.backgroundImage(for: .selected) == image)
_ = Observable.just(image).subscribe(button.rx.backgroundImage(for: .selected))
XCTAssertTrue(button.backgroundImage(for: .selected) == image)
}

func testBackgroundImageDefault() {
let button = UIButton(frame: CGRect(x: 0, y: 0, width: 1, height: 1))
let image = UIImage()

XCTAssertFalse(button.backgroundImage(for: []) == image)
_ = Observable.just(image).subscribe(button.rx.backgroundImage())
XCTAssertTrue(button.backgroundImage(for: []) == image)
}
}

#endif
Expand Down

0 comments on commit b4efef3

Please sign in to comment.