使用分段控制器更改视图 [英] Using Segmented Controller to Change Views

查看:95
本文介绍了使用分段控制器更改视图的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我知道之前已经问过这个问题,我知道如何实际更改视图或查看具有分段控件的控制器,但我有一个更具体的问题。我设计了我的UI,设计师使用了像UISegmentedControl这样的东西,作为视图转换器。但是,它位于导航栏下方,因此我无法在导航栏中嵌入分段控件,让导航控制器处理在堆栈上和窗口外推送和弹出视图,因为我需要分段控件不动画,就好像它是导航栏的一部分。我尝试过这几种方法,例如使用带有包含分段控件的子类导航栏的导航控制器,但是您无法编辑导航控制器导航栏的框架。如果有人可以帮助设置自定义动画,使得分段控件不会动画,那也会很棒。我正在使用下面的导航栏将图片链接到分段控件以获取上下文。
https://dl.dropboxusercontent.com/s/qjiedv4thpaosru/Screen%20Shot%202014-11-16%20at%201.30.45%20PM.png?dl=0



谢谢!



更新:



我会将此作为回答发布并选择它,如果这个问题没有被搁置,但这是我如何解决我自己的问题:



我创建了一个带有navBar +分段控件的UIViewController(如上所示) ),以及它下面的容器视图。



我拿了两个VC(测验和学习)并将它们放在导航控制器中。通过分段控件上的IBAction,我使用操纵导航堆栈来访问正确的视图控制器。

解决方案

假设你有一个ViewController,mainVC,嵌入在UINavigationController中,mainVC具有分段控件。选择第一个段时,您需要显示firstVC viewController,第二个段需要secondVC。



我发现如果将mainVC作为UIPageViewController的子类,它确实很困难从动画开/关屏幕停止分段控制。所以相反,我会创建一个UIPageViewController,并将其作为mainVC视图的子视图嵌入(它可以具有相同的框架)。分段控件同样是mainVC视图的子视图(因此您可以根据需要进行布局 - 例如,在导航栏的下方)。我在mainVC的viewDidLoad中的代码中完成了所有这些:

  //设置页面控制器来管理页面.. .. 
self.pageController = [[UIPageViewController alloc] initWithTransitionStyle:UIPageViewControllerTransitionStyleScroll navigationOrientation:UIPageViewControllerNavigationOrientationHorizo​​ntal 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]方向:UIPageViewControllerNavigationDirectionForward animated:NO completion:nil];
[self addChildViewController:self.pageController];
[self.view addSubview:self.pageController.view];
//并在顶部放置一个分段控件....
CGRect segmentFrame = CGRectMake(0,0,self.view.frame.size.width,50); //或者其他
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];

在您的segmentTapped:方法中,您可以触发pageViewController从firstVC交换到secondVC,反之亦然:

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

如果实现UIPageViewController的委托和数据源方法,您甚至可以使用滑动手势来切换页面。 / p>

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.

解决方案

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.

这篇关于使用分段控制器更改视图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆