0

I have the following scenario:

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

Ok, so, when user clicks on an option in the UINavigationViewController it pushes "pushedViewController", and in that viewcontroller I want to have a segmented control like the one on the App Store http://i.stack.imgur.com/hrdxL.png

How can I do this?

I already checked

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

This could help me but I can't figure out how Switch between UIViewControllers using UISegmentedControl

And this user asked the same but it doesn't have the full code Recreating Segmented Control from iPhone App Store

Community
  • 1
  • 1
estemendoza
  • 2,812
  • 4
  • 26
  • 47

1 Answers1

0

The pushedViewController can contain a UIScrollView framed to fill the screen. Set paging enabled, and give it three big subviews, like this:

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];
}
danh
  • 55,236
  • 10
  • 89
  • 124
  • Nice, I'll give it try but I think that this is going to be a little problematic because I want to add UIViewControllers that have UITableView and I don't know exactly how are they going to behave. – estemendoza May 06 '12 at 03:15
  • Yes. I meant to add something to my answer about that. This is for full screen views, controlled by the same view controller. 3 view controllers controlled by a segmented control is really a tab bar controller. If you want the tabs at the top, want them to look like a segmented button, want the VCs to slide, etc. Then you're basically deciding to implement your own container view controller (probably not even a tab bar vc subclass). This is a pretty large undertaking. Best of luck. – danh May 06 '12 at 03:20