Skip to content

Instantly share code, notes, and snippets.

Last active January 6, 2017 19:01
Show Gist options
  • Save timonus/452456f6790c2fb78723 to your computer and use it in GitHub Desktop.
Save timonus/452456f6790c2fb78723 to your computer and use it in GitHub Desktop.
// UIWindow+AppSwitchScrollStopper.h
// Created by Tim Johnsen on 3/27/16.
#import <UIKit/UIKit.h>
@interface UIWindow (AppSwitchScrollStopper)
/// Call this early on in your app's lifecycle to avoid
/// scroll-related flashing when your app resumes from the background
- (void)installAppSwitchScrollStopper;
// UIWindow+AppSwitchScrollStopper.m
// Created by Tim Johnsen on 3/27/16.
#import "UIWindow+AppSwitchScrollStopper.h"
@interface UIView (AppSwitchScrollStopper)
- (void)recursiveStopScrolling;
@implementation UIView (AppSwitchScrollStopper)
- (void)recursiveStopScrolling
if ([self isKindOfClass:[UIScrollView class]]) {
UIScrollView *scrollView = (UIScrollView *)self;
// Stop the scrolling (
[scrollView setContentOffset:scrollView.contentOffset animated:NO];
// Stop the scroll indicators from being captured
if (scrollView.showsHorizontalScrollIndicator) {
scrollView.showsHorizontalScrollIndicator = NO;
scrollView.showsHorizontalScrollIndicator = YES;
if (scrollView.showsVerticalScrollIndicator) {
scrollView.showsVerticalScrollIndicator = NO;
scrollView.showsVerticalScrollIndicator = YES;
for (UIView *view in self.subviews) {
[view recursiveStopScrolling];
@implementation UIWindow (AppSwitchScrollStopper)
+ (void)load
void (^stopScrollingBlock)(NSNotification *_Nonnull notification) = ^(NSNotification *_Nonnull notification) {
for (UIWindow *window in [self windowsToStopScrolling]) {
[window recursiveStopScrolling];
[[NSNotificationCenter defaultCenter] addObserverForName:UIApplicationDidEnterBackgroundNotification object:nil queue:[NSOperationQueue mainQueue] usingBlock:stopScrollingBlock];
// Works in iOS extensions too!
if (&NSExtensionHostDidEnterBackgroundNotification) {
[[NSNotificationCenter defaultCenter] addObserverForName:NSExtensionHostDidEnterBackgroundNotification object:nil queue:[NSOperationQueue mainQueue] usingBlock:stopScrollingBlock];
+ (NSMutableSet<UIWindow *> *)windowsToStopScrolling
static NSMutableSet<UIWindow *> *windowsToStopScrolling = nil;
static dispatch_once_t onceToken;
dispatch_once(&onceToken, ^{
windowsToStopScrolling = [NSMutableSet new];
return windowsToStopScrolling;
- (void)installAppSwitchScrollStopper
[[[self class] windowsToStopScrolling] addObject:self];
Copy link

That is very cool, thanks!
Maybe it will be cool to add prefixes for all methods so someone won't be in trouble if they will be copy-pasting that into their projects

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment