【问题标题】:UISegmentedControl switch views in uitableviewcontrollerUISegmentedControl 在 uitableviewcontroller 中切换视图
【发布时间】:2012-05-15 01:33:13
【问题描述】:

我有以下场景:

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

好的,所以,当用户单击 UINavigationViewController 中的一个选项时,它会推送“pushedViewController”,并且在该视图控制器中,我希望有一个分段控件,就像 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

【问题讨论】:

    标签: iphone uisegmentedcontrol


    【解决方案1】:

    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];
    }
    

    【讨论】:

    • 很好,我会试一试,但我认为这会有点问题,因为我想添加具有 UITableView 的 UIViewControllers,但我不知道它们将如何表现.
    • 是的。我的意思是在我的回答中添加一些内容。这适用于全屏视图,由同一个视图控制器控制。 3 由分段控件控制的视图控制器实际上是一个标签栏控制器。如果您希望选项卡位于顶部,希望它们看起来像一个分段按钮,希望 VC 滑动等等。那么您基本上决定实现自己的容器视图控制器(甚至可能不是选项卡栏 vc 子类)。这是一项相当大的事业。祝你好运。
    猜你喜欢
    • 2011-07-13
    • 1970-01-01
    • 2015-09-23
    • 2010-11-06
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-02-25
    相关资源
    最近更新 更多