SBSegmentedViewController 1.1.2

SBSegmentedViewController 1.1.2

TestsTested
LangLanguage Obj-CObjective C
License MIT
ReleasedLast Release Dec 2014

Maintained by Scott Berrevoets.



  • By
  • Scott90

SBSegmentedViewController is a custom view controller container that uses a segmented control to switch between view controllers. It can be used with iOS 5.0+, though the deployment target for the sample project is set to iOS 6.0.

Use

SBSegmentedViewController is very easy to use, and can be set up either using a storyboard or programmatically. It sets up a container view controller that switches between its child view controllers with a UISegmentedControl. The segmented control can be placed in either the navigation bar or the toolbar of the navigation controller that contains SBSegmentedViewController. This position can be set using the position property (for which the options are SBSegmentedViewControllerPositionNavigationBar or SBSegmentedViewControllerPositionToolbar).

Storyboard

Follow these steps to instantiate SBSegmentedViewController from a storyboard:

  1. Drag out a view controller and set its class to SBSegmentedViewController
  2. Embed the SBSegmentedViewController in a navigation controller
  3. Drag out more view controllers that you want as part of SBSegmentedViewController
  4. Fill in all the view controllers' title properties in the storyboard. This is a requirement, as the title attribute is used for setting the segment's title.
  5. Control-drag from the SBSegmentedViewController scene to a view controller that you created in step 2
  6. Choose the embedded segment segue. This is a custom segue that's very similar to the embedded segue introduced in iOS 6.
  7. Give the segue an identifier
  8. Repeat steps 4-6 for all the view controllers you added in step 2
  9. Call -[SBSegmentedViewController addStoryboardSegments:] passing in an NSArray of NSStrings that match the segue identifiers

Programmatically

The code below is a sample use of SBSegmentedViewController in application:didFinishLaunchingWithOptions: when created programmatically:

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
    self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];

    MyViewController *myVC1 = [[MyViewController alloc] initWithNibName:@"MyVC1" bundle:nil];
    MyOtherViewController *myVC2 = [[MyViewController alloc] initWithNibName:@"MyVC2" bundle:nil];

    SBSegmentedViewController *segmentedController = [[SBSegmentedViewController alloc] initWithViewControllers:@[myVC1, myVC2]];
    // Or, we could choose our own titles:
    // SBSegmentedViewController *segmentedController = [[SBSegmentedViewController alloc] initWithViewControllers:@[myVC1, myVC2] titles:@[@"Segment 1", @"Segment 2"]];
    self.window.rootViewController = [[UINavigationController alloc] initWithRootViewController:segmentedController];

    [self.window makeKeyAndVisible];
}

Of course you don't have to use SBSegmentedViewController as the root view controller, you can push it onto a navigation controller, present it modally, or do anything else that you can do with a normal view controller.

License

The MIT license applies to the code distributed in this repo.