title: Sliding Between UISegmentedControl Views
body: <p>Currently I am wanting to create a slide animation when the user selects on a segmented button of a UISegmentedControl instantiated on top of a navigationbar. Currently I have a UISegmentedControl with 6 buttons the user is allowed to press and select to go to different views.</p>
  
  <p>Everything works accordingly but I am having an issue with implementing the slide transition, if it is even possible.</p>
  
  <p>I am able to implement a slide transition between UITabBar views using this method:</p>
  
  <pre><code>- (BOOL)tabBarController:(UITabBarController *)tabBarController shouldSelectViewController:(UIViewController *)viewController
  {
      int controllerIndex = [[tabBarController viewControllers] indexOfObject:viewController];
  
      if(controllerIndex == self.selectedIndex || self.isAnimating){
          return NO;
      }
  
      // Get the views.
      UIView * fromView = tabBarController.selectedViewController.view;
      UIView * toView = [viewController view];
  
      // Get the size of the view area.
      CGRect viewSize = fromView.frame;
      BOOL scrollRight = controllerIndex &gt; tabBarController.selectedIndex;
  
      // Add the to view to the tab bar view.
      [fromView.superview addSubview:toView];
  
      // Position it off screen.
      toView.frame = CGRectMake((scrollRight ? 320 : -320), viewSize.origin.y, 320, viewSize.size.height);
  
      [UIView animateWithDuration:0.2 animations: ^{
  
          // Animate the views on and off the screen. This will appear to slide.
          fromView.frame =CGRectMake((scrollRight ? -320 : 320), viewSize.origin.y, 320, viewSize.size.height);
          toView.frame =CGRectMake(0, viewSize.origin.y, 320, viewSize.size.height);
  
      } completion:^(BOOL finished) {
  
          if (finished) {
              // Remove the old view from the tabbar view.
              [fromView removeFromSuperview];
              tabBarController.selectedIndex = controllerIndex;
          }
      }
       ];
  
      return NO;
  }
  </code></pre>
  
  <p>Not so sure if the same rules apply for a UISegmentedControl of several viewcontrollers. Is this possible to do? I figure it should be but anyone have any ideas on how to get started?</p>
  
  <p><strong>EDIT</strong> </p>
  
  <p>Heres the code I use within my segmentedcontroller...</p>
  
  <pre><code>- (void)viewDidLoad
  {
      [super viewDidLoad];
      // Do any additional setup after loading the view.
  
      /* This bunch of code creates the segmentedControllerButtons in the nav bar */
      self.segmentedViewControllers = [self segmentedViewControllerContent];
  
      NSArray * segmentTitles = @[@"Plant", @"Net", @"Wiz", @"Date", @"Clone", @"GF/E"];
  
      self.segmentedControl = [[UISegmentedControl alloc]initWithItems:segmentTitles];
      self.segmentedControl.selectedSegmentIndex = 0;
      self.segmentedControl.segmentedControlStyle = UISegmentedControlStyleBar;
  
      [self.segmentedControl addTarget:self action:@selector(didChangeSegmentControl:) forControlEvents:UIControlEventValueChanged];
  
      self.navigationItem.titleView = self.segmentedControl;
      self.navigationController.navigationBar.tintColor = [UIColor blackColor];
      self.segmentedControl.tintColor = [UIColor redColor];
      [self didChangeSegmentControl:self.segmentedControl]; // kick everything off
  
  }
  
  - (void)didReceiveMemoryWarning
  {
      [super didReceiveMemoryWarning];
      // Dispose of any resources that can be recreated.
  }
  
  - (NSArray *)segmentedViewControllerContent {
  
      CDDConfigPlantViewController *plant = [[CDDConfigPlantViewController alloc] initWithNibName:@"CDDConfigPlantViewController" bundle:nil];
      plant-&gt;ipAddress = ipAddress;
      plant-&gt;encode = encodedInfo;
      CDDConfigNetworkViewController *network = [[CDDConfigNetworkViewController alloc] initWithNibName:@"CDDConfigNetworkViewController" bundle:nil];
      network-&gt;ipAddress = ipAddress;
      network-&gt;encode = encodedInfo;
      CDDConfigAcquisitionWizardViewController *acquisition_wizard = [[CDDConfigAcquisitionWizardViewController alloc] initWithNibName:@"CDDConfigAcquisitionWizardViewController" bundle:nil];
      acquisition_wizard-&gt;ipAddress = ipAddress;
      acquisition_wizard-&gt;encode = encodedInfo;
      CDDConfigDateTimeViewController *date_time = [[CDDConfigDateTimeViewController alloc] initWithNibName:@"CDDConfigDateTimeViewController" bundle:nil];
      date_time-&gt;ipAddress = ipAddress;
      date_time-&gt;encode = encodedInfo;
      CDDConfigCDDCloneViewController *cdd_clone = [[CDDConfigCDDCloneViewController alloc] initWithNibName:@"CDDConfigCDDCloneViewController" bundle:nil];
      cdd_clone-&gt;ipAddress = ipAddress;
      cdd_clone-&gt;encode = encodedInfo;
      CDDConfigGroundfaultEnergyViewController *groundfault_energy = [[CDDConfigGroundfaultEnergyViewController alloc] initWithNibName:@"CDDConfigGroundfaultEnergyViewController" bundle:nil];
      groundfault_energy-&gt;ipAddress = ipAddress;
      groundfault_energy-&gt;encode = encodedInfo;
  
      NSArray * controllers = [NSArray arrayWithObjects:plant, network, acquisition_wizard, date_time, cdd_clone, groundfault_energy, nil];
  
      return controllers;
  }
  
  
  
  #pragma mark -
  #pragma mark Segment control
  
  - (void)didChangeSegmentControl:(UISegmentedControl *)control {
      if (self.activeViewController) {
          [self.activeViewController viewWillDisappear:NO];
          [self.activeViewController.view removeFromSuperview];
          [self.activeViewController viewDidDisappear:NO];
      }
  
  
      self.activeViewController = [self.segmentedViewControllers objectAtIndex:control.selectedSegmentIndex];
      [self.activeViewController viewWillAppear:YES];
      [self.view addSubview:self.activeViewController.view];
      [self.activeViewController viewDidAppear:YES];
  }
  
  - (void)viewWillAppear:(BOOL)animated {
      [super viewWillAppear:animated];
      [self.activeViewController viewWillAppear:animated];
  }
  
  - (void)viewDidAppear:(BOOL)animated {
      [super viewDidAppear:animated];
      [self.activeViewController viewDidAppear:animated];
  }
  
  - (void)viewWillDisappear:(BOOL)animated {
      [super viewWillDisappear:animated];
      [self.activeViewController viewWillDisappear:animated];
  }
  
  - (void)viewDidDisappear:(BOOL)animated {
      [super viewDidDisappear:animated];
      [self.activeViewController viewDidDisappear:animated];
  }
  </code></pre>