Skip to content

Swipe between ViewControllers like in the Spotify or Twitter app with an interactive Segmented Control in the Navigation Bar

License

Notifications You must be signed in to change notification settings

cwRichardKim/RKSwipeBetweenViewControllers

Repository files navigation

RKSwipeBetweenViewControllers

UIPageViewController and custom UISegmentedControl synchronized and animated. Similar to Spotify's "My Music" section.

Please check the .h to see how to customize anything

Support

Pod

You should not use the pod in most cases, as they don't allow for customizability. I would recommend dragging the .h and .m files manually into your project

pod 'RKSwipeBetweenViewControllers'

Updates, Questions, and Requests

twitter <--- I am a very light twitterer, so I won't spam you

Demo:

(after five minutes of customization)

demo

Any number of any view controllers should technically work, though it doesn't look great with more than 4

Customizable!

Customizable!

(check the RKSwipeBetweenViewControllers.h for actual customizable features)

how to use

(check out the provided AppDelegate to see an example):

Programmatically (preferred)

  1. Import RKSwipeBetweenViewControllers.h

    #import <RKSwipeBetweenViewControllers/RKSwipeBetweenViewControllers.h>
  2. Initialize a UIPageViewController

    UIPageViewController *pageController = [[UIPageViewController alloc] initWithTransitionStyle:UIPageViewControllerTransitionStyleScroll navigationOrientation:UIPageViewControllerNavigationOrientationHorizontal options:nil];
  3. Initialize a RKSwipeBetweenViewControllers

    RKSwipeBetweenViewControllers *navigationController = [[RKSwipeBetweenViewControllers alloc]initWithRootViewController:pageController];
  4. Add all your ViewControllers (in order) to navigationController.viewControllerArray (try to keep it under 5)

    [navigationController.viewControllerArray addObjectsFromArray:@[viewController1, viewController2, viewController3]];
  5. Use the custom class (or call it as the first controller from app delegate: see below)

    self.window = [[UIWindow alloc] initWithFrame:UIScreen.mainScreen.bounds];
    self.window.rootViewController = navigationController;
    [self.window makeKeyAndVisible];

StoryBoard (do not use pods for this one)

  1. Drop the file into your project and import RKSwipeBetweenViewControllers.h

    #import RKSwipeBetweenViewControllers.h
  2. Embed a UIPageViewController inside a UINavigationController. Change the class of the to UINavigationController the custom class (RKSwipeBetweenViewControllers)

  3. change the transition style of the pageviewcontroller to scroll (click on the UIPageViewController in storyboard -> attributes inspector -> transition style -> scroll)

  4. go to the RKSwipeBetweenViewControllers.m file and use it as your own class now. Add your view controllers to "viewControllerArray". See below for various options.

    Programmatically, outside RKSwipeBetweenViewControllers.m (if this navigation bar isn't the first screen that comes up, or if you want to call it from the delegate)

    [customNavController.viewControllerArray addObjectsFromArray:@[viewController1, viewController2, viewController3]];

    Programmatically, inside RKSwipeBetweenViewControllers.m (most cases if your view controllers are programmatically created)

    [viewControllerArray addObjectsFromArray:@[demo,demo2]];

    storyboard, inside RKSwipeBetweenViewControllers.m (if your viewcontrollers are on the storyboard, but make sure to give them storyboard IDs)

    UIStoryboard *storyboard = [UIStoryboard storyboardWithName:@"Main" bundle:nil];
        UIViewController* theController = [storyboard instantiateViewControllerWithIdentifier:@"storyboardID"];
    
        [viewControllerArray addObject:theController];

    storyboard, outside RKSwipeBetweenViewControllers.m (if your viewcontrollers are on the storyboard, but make sure to give them storyboard IDs)

    UIStoryboard *storyboard = [UIStoryboard storyboardWithName:@"Main" bundle:nil];
        UIViewController* theController = [storyboard instantiateViewControllerWithIdentifier:@"storyboardID"];
    
        [theCustomViewController.viewControllerArray addObject:theController];

Any problems/questions? shoot me a pm

Areas for Improvement / Involvement

  • Working with horizontal layout
  • Working with more than 5 pages
  • Handful of infrequent bugs
  • Better performance when loading pages
  • Changing layout away from UINavigationController to allow the bar to be at the bottom
  • Bug: adding a MKMapView to a UIViewController in storyboard causes strange visual bug. Adding programmatically is fine
  • Crash on load for UITabBarControllers (resolved): #15

About

Swipe between ViewControllers like in the Spotify or Twitter app with an interactive Segmented Control in the Navigation Bar

Resources

License

Stars

Watchers

Forks

Packages

No packages published