Skip to Content

Scroll to Top with Undo in RxSwift

Posted on 9 mins read

In this post I’ll write about how to implement a custom scroll to top feature with the ability to restore the old contentOffset. The first app I saw implementing this feature was TweetBot in its 4.8 update and it became instantly a must have for me.

While working on side-project application (stay tuned πŸ˜‰), I implemented this feature as well. Let’s see how it can be done entirely using RxSwift 🀘

✨ RxSwift

My love for RxSwift began mid 2016 when I joined Heetch. Since then, it helps me to write complex features in such a simple, expressive, and readable way. I think I will speak about RxSwift often on this blog, because IMHO it definitely helps to write elegant code.

The scroll to top is usually triggered by a tap on the status bar, but as it will be implemented here it will also be possible to add new sources to trigger. For instance a tap on tab bar item, or on viewWillAppear(), or on everything else as soon as it’s an Observable.

The beauty of RxSwift is to offer a uniform interface for many Cocoa's design patterns (delegate, target/action, notifications, callback closures, etc.).


The recipe

  1. Implement an Observable<Void> that emits whenever the user taps on the UIApplication.shared.keyWindow in status bar’s frame
  2. Associate 1. to a UIViewController and filter its events to emit them if and only if the UIViewController instance is visible (ie. between viewDidAppear and viewWillDisappear lifecycle events)
  3. Implement a ScrollTarget enum to let switch over different target (either .top or .offset(CGFloat)
  4. Implement an Observable that emits whenever the user has finished to scroll an UIScrollView in order to save the current contentOffset into ScrollTarget.offset(contentOffset.y)
  5. Implement the final subscription that combine 2. and 4. to scroll the UIScrollView to the desired target.


For the implementation I used RxSwift, RxCocoa and RxSwiftExt. There are also two little Rx extensions I use.

The first one transforms any Observable<E> into Observable<Void>. It’s quite convenient when we don’t need the value. Typically when you use the Observable as a sampler.

1extension ObservableType {
2  func void() -> Observable<Void> {
3    return map { _ in }
4  }

The second is a startWith operator that takes a closure instead of a value. It avoids a strong reference on the initial value.

1extension ObservableType {
2  func startWith(_ factory: @escaping () -> Observable<E>) -> Observable<E> {
3    let start = Observable<E>.deferred {
4      factory()
5    }
6    return start.concat(self)
7  }

1. Detect tap on status bar

To do this without any subclassing, RxCocoa will be a precious help.

First let’s make an Observable<UIWindow?> that emits the keyWindow of UIApplication.shared.

 1extension Reactive where Base: UIApplication {
 2  var keyWindow: Observable<UIWindow?> {
 3    return NotificationCenter.default.rx
 4      .notification(.UIWindowDidBecomeKey, object: nil)
 5      .map { notification -> UIWindow? in
 6        notification.object as? UIWindow
 7      }
 8      .startWith { [weak base] in
 9        guard let base = base else { return .empty() }
10        return .just(base.keyWindow)
11      }
12  }
  • On lines 3 to 6 we listen for UIWindowDidBecomeKey notification and get the associated object (the window) once a notification is posted
  • On lines 8 to 11 we use the current base.keyWindow as a start value

Now that we always have the latest keyWindow we can flatMap over it to detect when user taps in it. The best way to do this is to attach an UITapGestureRecognizer to the window. It would be really easy to do with RxGesture for example.

Unfortunately, on iOS the view system won’t deliver the touch event to any gesture recognizer if the touch location is in status bar’s frame. The only way I found to bypass this limitation is to intercept the invocation of:

1func hitTest(_ point: CGPoint, with event: UIEvent?) -> UIView?

And RxCocoa has a powerful built-in .methodInvoked() operator to do this.

 1extension Reactive where Base: UIApplication {
 2  var statusBarTap: Observable<Void> {
 3    return keyWindow
 4      .flatMapLatest { window -> Observable<CGPoint> in
 5        guard let window = window else { return .empty() }
 6        return window.rx
 7          .methodInvoked(#selector(UIView.hitTest(_:with:)))
 8          .map { args -> CGPoint? in
 9            guard args.count == 2,
10              let point = args[0] as? CGPoint,
11              let _ = args[1] as? UIEvent
12              else { return nil}
13            return point
14          }
15          .unwrap()
16      }
17      .filter { [unowned app = self.base] point in
18        point.y < app.statusBarFrame.maxY + 20
19      }
20      .void()
21      .debounce(0, scheduler: MainScheduler.asyncInstance)
22  }
  • On line 3 we use the keyWindow: Observable<UIWindow?> defined earlier
  • On line 7 we use the .methodInvoked() operator to intercept the invocation of UIView.hitTest(_:with:),
  • On lines 8 to 14 we map the previous result to get the point location of the touch event. In practice, it would be safe to return directly a forced unwrapped arg.first as! CGPoint because we know the exact method signature, but I still prefer to keep an optional
  • On line 11, we make sure that there’s really an event given in order to avoid false positives
  • On line 15 we unwrap the point with .unwrap() operator of RxSwiftExt.
  • On line 18 you can notice that I add an extra 20pt to the statusBarFrame. It makes the tappable target a little bit higher. M. Fitts approves it πŸ‘.
  • On line 21, we use the .debounce() operator with a delay of 0 and an async instance of the MainScheduler. It’s important because UIView.hitTest(_:with:) will be called many times during the same run loop, so we need to filter repetitive events. You can see this as similar to an other UIKit pattern like setNeedsDisplay() / displayIfNeeded()

πŸ’ͺ Awesome, we’re done with the first step.

2. Detect status bar tap on a visible ViewController

As you will likely use this feature on a UIScrollView included in a specific UIViewController, you better make sure that this UIViewController is actually visible before reacting to this event.

Otherwise, imagine you have several UIViewController implementing this gesture in a UITabBarController. If you don’t emit the event only for the visible UIViewController, a tap on the status bar will scroll to top all UIScrollView of all view controllers. We obviously don’t want this.

 1extension Reactive where Base: UIViewController {
 2  var statusBarTap: Observable<Void> {
 3    let isVisible: Observable<Bool> = Observable
 4    .merge(
 5      methodInvoked(#selector(
 6      	UIViewController.viewWillAppear(_:)
 7      )).map(to: false),
 9      methodInvoked(#selector(
10      	UIViewController.viewDidAppear(_:)
11      )).map(to: true),
13      methodInvoked(#selector(
14      	UIViewController.viewWillDisappear(_:)
15      )).map(to: false),
17      methodInvoked(#selector(
18      	UIViewController.viewDidDisappear(_:)
19     	)).map(to: false)
20    )
21    return UIApplication.shared.rx
22      .statusBarTap
23      .pausable(isVisible)
24  }

Once again, RxCocoa’s .methodInvoked() operator is a great help as it allows us to intercept appearance lifecycle methods and map them to a boolean indicating if the view controller is visible or not. Here, viewDidAppear is mapped to true (line 11) and other methods are mapped to false.

To finish, we reuse UIApplication.shared.rx.statusBarTap we created earlier and use the .pausable() operator of RxSwiftExt in order to emit values only if latest value from isVisible is true.

3. ScrollTarget

1enum ScrollTarget {
2  case top
3  case offset(CGFloat)

βœ… Done

4. Save contentOffset after scroll

Starting from here, I will simplify and write all the code we need in our UIViewController’s viewDidLoad(). I will also assume there are a scrollView and a disposeBag around there.

Let’s start with the code.

 1func viewDidLoad() {
 2  super.viewDidLoad()
 4  let target = BehaviorSubject(value:
 6  let source = self.rx.statusBarTap.withLatestFrom(target).share()
 8  // Save
 9  source
10    .map { [unowned scrollView] target -> ScrollTarget in
11      switch target {
12      case .top:
13        return .offset(scrollView.contentOffset.y)
14      case .offset:
15        return .top
16      }
17    }
18    .bind(to: target)
19    .disposed(by: disposeBag)
21  // Reset
22  scrollView.rx
23    .willBeginDragging
24    .map(to: .top)
25    .bind(to: target)
26    .disposed(by: disposeBag)
28  // To be continued...
  • On line 4 we create a BehaviorSubject that will hold our next ScrollTarget. The initial target will obviously be .top.
  • On line 6 we prepare our source. It’s just the UIViewController.rx.statusBarTap we created earlier, combined with the next target, and we finish with a share(). It’s important to share here because as on line 27 we update the target, we want to be sure that the subscription to actually scrolls the scroll view, use the correct target.
  • On lines 8 to 19 we save the next target. If current target was .top, then the next target will be .offset with the current scrollView offset. Otherwise, the next target will be .top. This allows us to alternatively use one target or the other.
  • On lines 22 to 28 we add a mechanism that reset the next target to .top as soon as the user interacts with the scrollView, because it wouldn’t make sense to restore the old offset.

5. The final piece

Now we can implement the actual scrolling.

 1func viewDidLoad() {
 2  // ...
 4  let source = ...
 6  // Save
 7  // ...
 9  // Reset
10  // ...
12  source
13    .map { target -> CGFloat in
14      switch target {
15      case .top:
16        return
17      case .offset(let offset):
18        return offset
19      }
20    }
21    .subscribe(onNext: { [unowned scrollView] offset in
22        var contentOffset = scrollView.contentOffset
23        contentOffset.y = offset
24        scrollView.setContentOffset(contentOffset, animated: true)
25    })
26    .disposed(by: disposeBag)

No big deal here, we just get the good offset for each ScrollTarget cases and we animate the scrollView.contentOffset update.

That’s all πŸŽ‰

To conclude, we’ve seen some interesting techniques offered by RxSwift and RxCocoa that allowed us to compose an interesting feature without subclassing, or using a mutable shared state.

As an exercise, you can factorize the code we added in the viewDidLoad() in order to make it easily reusable on any UIViewController / UIScrollView.

⚠️ Despite how elegant and clean the final code looks like, there are still some trade-offs because we use some RxCocoa features that depends on Objective-C runtime and, event if we don’t use any private methods, you still should be careful when you use such techniques.

I hope you enjoyed reading this blog post / tutorial. Please do not hesitate to add a comment to tell me what you thought about it, to ask me some questions, or even to suggest me an idea for a future post where I could try to make an obscure solution more elegant πŸ˜‰
comments powered by Disqus