0

I am aware that this question has been asked previously, and I know how to actually change views or view controllers w/ a segmented control, but I have a more specific problem. I had my UI designed, and the designer used something that appeared to me like a UISegmentedControl as the view changer. However, it is below the navigation bar, so I cannot embed the Segmented Control in the navigation bar and let the navigation controller handle pushing and popping views on and off the stack, as I need the segmented control to not animate, as if it were part of the Navigation Bar. I have tried this a few ways, like using a navigation controller with a subclassed navigation bar that contains a segmented control, but you cannot edit the frame of a navigation controller's navigation bar. If anyone could help set up a custom animation such that the segmented control wouldn't animate, that would be great too. I am linking a picture to the segmented control with the navigation bar below for context. https://dl.dropboxusercontent.com/s/qjiedv4thpaosru/Screen%20Shot%202014-11-16%20at%201.30.45%20PM.png?dl=0

Thanks!

UPDATE:

I would post this as an answer and select it if this question weren't on hold, but here is how I solved my own problem:

I created a UIViewController with the navBar+segmented control (shown above), and a container view under it.

I took the two VC's (Quiz and Study) and placed them in a navigation controller. With an IBAction on the segmented control, I use manipulate the navigation stack to access the correct view controller.

ericmarkmartin
  • 717
  • 1
  • 8
  • 19
  • Would you be OK to use a UIPageViewController to swap VCs when the segmented control is tapped (you can still use the navigation controller to push/pop subsequent VCs)? I did something similar with a UIPageControl, but I'm sure it could work with a UISegmentedControl. – pbasdf Nov 16 '14 at 19:02
  • Do you mean use a PageViewController to swap views? I guess that could work, but I'd prefer to have a separate view controller so the logic for the different views is separate. If I used a pagviewcontroller to swap view controllers, wouldn't the segmented control animate? If you think it would work, do you think you could give me some more implementation details? – ericmarkmartin Nov 16 '14 at 19:05
  • I'll outline it in an answer - it's a little too long to explain for comments! – pbasdf Nov 16 '14 at 19:13
  • Why are you using a navigation controller? I think you'd want a tab bar controller for this. If you do that, simply hide the tab bar, provide your own segment control, and use that to tell the tab bar controller which panel to select. – Anna Dickinson Nov 16 '14 at 19:45
  • I think you misunderstood the problem. Using a tab bar controller would still animate the change of segmented controller. – ericmarkmartin Nov 16 '14 at 20:26

1 Answers1

2

Assume you have a ViewController, mainVC, embedded in a UINavigationController, and mainVC has the segmented control. When the first segment is selected you want firstVC viewController presented, and secondVC for the second segment.

I found that if you make mainVC a subclass of UIPageViewController, it does prove difficult to stop the segmented control from animating on/off screen. So instead, I would create a UIPageViewController, and embed it as a subView of the mainVC's view (it can have the same frame). The segmented control is likewise a subview of mainVC's view (so you can lay it out however you like - just below the navigation bar, for example). I did all of this in code in the viewDidLoad of mainVC:

// Set up a page controller to manage the pages....
self.pageController = [[UIPageViewController alloc] initWithTransitionStyle:UIPageViewControllerTransitionStyleScroll navigationOrientation:UIPageViewControllerNavigationOrientationHorizontal options:nil];
// self.pageController.dataSource = self;
// self.pageController.delegate = self;
self.pageController.view.frame = CGRectMake(0,0,self.view.frame.size.width, self.view.frame.size.height);
[self.pageController setViewControllers:@[firstVC] direction:UIPageViewControllerNavigationDirectionForward animated:NO completion:nil];
[self addChildViewController:self.pageController];
[self.view addSubview:self.pageController.view];
// And lay a segmented control over the top....
CGRect segmentFrame = CGRectMake(0,0,self.view.frame.size.width,50); // or whatever
self.segmentedControl = [[UISegmentedControl alloc] initWithItems:@[@"First",@"Second"]];
[self.segmentedControl addTarget:self action:@selector(segmentTapped:) forControlEvents:UIControlEventValueChanged];
self.segmentedControl.frame = segmentFrame;
[self.view addSubview:self.segmentedControl];

In your segmentTapped: method, you can trigger the pageViewController to swap from firstVC to secondVC or vice versa:

[self.pageController setViewControllers:@[secondVC] direction:UIPageViewControllerNavigationDirectionForward animated:YES completion:nil];

If you implement the UIPageViewController's delegate and datasource methods, you can even have swipe gestures to switch pages.

pbasdf
  • 21,386
  • 4
  • 43
  • 75