devxoul / RxViewController

RxSwift wrapper for UIViewController and NSViewController
MIT License
346 stars 64 forks source link

Access UIBarButtonItems tap? #8

Closed Sajjon closed 4 years ago

Sajjon commented 6 years ago

Hi! Would it be possible to extend RxViewController tap events of the optional leftBarButtonItem and rightBarButtonItem?

    var leftBarButtonTapped: Driver<Void> { get }
    var rightBarButtonTapped: Driver<Void> { get }

It would be awesome to listen to these!

Sajjon commented 6 years ago

If using custom inside the barButtonItem, maybe something like:

extension Reactive where Base: UIViewController {
    public var leftBarButtonTapped: ControlEvent<Void> {
        guard let button = self.base.navigationItem.leftBarButtonItem?.customView as? UIButton else { return ControlEvent(events: Observable.empty()) }
        return button.rx.tap
    }

    public var rightBarButtonTapped: ControlEvent<Void> {
        guard let button = self.base.navigationItem.rightBarButtonItem?.customView as? UIButton else { return ControlEvent(events: Observable.empty()) }
        return button.rx.tap
    }
}

credit goes to @oscarsilver

devxoul commented 6 years ago

It would not work if the left bar button is not a custom button. RxCocoa already supports UIBarButtonItem+Rx so you may use that.