Using Segmented Controller to Change Views [closed

2020-05-07 01:57发布

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.

1条回答
唯我独甜
2楼-- · 2020-05-07 02:32

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.

查看更多
登录 后发表回答