uitableeconmented中的UISegmentedControl切换视图

时间:2012-05-05 23:46:21

标签: iphone uisegmentedcontrol

我有以下情况:

UITabBarController
--tab1
--tab2
----UINavigationController (with a UITableViewController and 3 rows)
------pushedViewController
--tab3
--tab4
--tab5

好的,所以,当用户点击UINavigationViewController中的一个选项时,它会推送“pushViewController”,并且在该视图控制器中,我想要一个像App Store http://i.stack.imgur.com/hrdxL.png上那样的分段控件

我该怎么做?

我已经检查了

https://stackoverflow.com/questions/1559794/changing-views-from-uisegmentedcontrol
https://stackoverflow.com/questions/5280653/uisegmentedcontrol-to-switch-views-in-uitabbarcontroller
http://www.astroryan.com/?p=19
https://stackoverflow.com/questions/8723094/using-uisegmentedcontrol-to-switch-between-two-views

这可以帮助我,但我无法弄清楚如何 Switch between UIViewControllers using UISegmentedControl

此用户问了相同的内容,但它没有完整的代码 Recreating Segmented Control from iPhone App Store

1 个答案:

答案 0 :(得分:0)

pushViewController可以包含一个框架的UIScrollView来填充屏幕。设置分页启用,并为其提供三个大的子视图,如下所示:

CGFloat width = self.view.bounds.width;
CGFloat height = self.view.bounds.height;

// it's easier to do all this this in xib/storyboard, but to illustrate...

self.scrollView.frame = CGRectMake(0,0,width,height);
self.scrollView.contentSize = CGSizeMake(width*3.0, height);  // this must be done in code
self.scrollView.pagingEnabled = YES;

UIView *subviewA, *subviewB, subviewC;

subviewA.frame = CGRectMake(0.0, 0.0, width, height);
subviewB.frame = CGRectMake(0.0, width, width, height);
subviewB.frame = CGRectMake(0.0, 2.0*width, width, height);

[self.scrollView addSubview:subviewA];
[self.scrollView addSubview:subviewB];
[self.scrollView addSubview:subviewC];

// then when the segmented control changes ...

- (IBAction)segmentedControlChanged:(UISegmentedControl *)sender {

    NSInteger page = sender.selectedSegmentIndex;
    CGFloat contentOffsetX = page*self.view.bounds.width;
    [self.scrollView setContentOffset:CGPointMake(contentOffsetX, 0.0) animated:YES];
}