在scrollview中添加现有视图控制器

时间:2014-08-12 01:24:40

标签: ios xcode uiscrollview

我有两个视图控制器,我想将其添加为我的scrollview的子视图,但是,当我添加这些视图控制器时,仅显示第二个控制器。

这是我的代码。

- (void)viewDidLoad
{
    UIScrollView *scrollView = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 0, 320, 460)];
    [self.view addSubview:scrollView];

    [scrollView setBounces:NO];

    scrollView.pagingEnabled = YES;
    scrollView.contentSize = CGSizeMake(320*2, 460);

    controllers = [[NSMutableArray alloc] initWithCapacity:0];

    First *first = [self.storyboard instantiateViewControllerWithIdentifier:@"First"];
    [scrollView addSubview:first.view];
    [controllers addObject:first];

    Second *second = [self.storyboard instantiateViewControllerWithIdentifier:@"Second"];
    [scrollView addSubview:second.view];
    [controllers addObject:second];
}

提前致谢。

1 个答案:

答案 0 :(得分:1)

举个例子,您可以根据自己的要求调整框架。

- (void)viewDidLoad
{
    UIScrollView *scrollView = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 0, 320, 460)];
    [self.view addSubview:scrollView];

    [scrollView setBounces:NO];

    scrollView.pagingEnabled = YES;
    scrollView.contentSize = CGSizeMake(320*2, 460);

    controllers = [[NSMutableArray alloc] initWithCapacity:0];

    First *first = [self.storyboard instantiateViewControllerWithIdentifier:@"First"];
    [scrollView addSubview:first.view];
    first.view.frame = CGRectMake(0.0, 0.0, 320.0, 460.0) ;
    [controllers addObject:first];

    Second *second = [self.storyboard instantiateViewControllerWithIdentifier:@"Second"];
    [scrollView addSubview:second.view];
    second.view.frame = CGRectMake(320.0, 0.0, 320.0, 460.0) ;
    [controllers addObject:second];
}