ScrollView内容大小

时间:2016-09-05 20:06:27

标签: objective-c uiview uiviewcontroller uiscrollview

我有一个包含三个视图的ScrollView。

我的目的是制作一个具有特定几何形状的ScrollView。 我只想在第二个视图中进行垂直滚动。

UIViewController* VC1=[[UIViewController alloc]initWithNibName:@"ViewController1" bundle:nil];
    [self addChildViewController:VC1];
    [self.scrollView addSubview:VC1.view];
    [VC1 didMoveToParentViewController:self];

UIViewController* VC2=[[UIViewController alloc]initWithNibName:@"ViewController2" bundle:nil];
[self addChildViewController:VC2];
[self.scrollView addSubview:VC2.view];
[VC2 didMoveToParentViewController:self];

CGRect VC2frame=VC2.view.frame;
VC2frame.origin.x=self.view.frame.size.width;
VC2.view.frame=VC2frame;

UIViewController* VC3=[[UIViewController alloc]initWithNibName:@"ViewController3" bundle:nil];
[self addChildViewController:VC3];
[self.scrollView addSubview:VC3.view];
[VC3 didMoveToParentViewController:self];

CGRect VC3frame=VC3.view.frame;
VC3frame.origin.x=self.view.frame.size.width;
VC3frame.origin.y=self.view.frame.size.height;
VC3.view.frame=VC3frame;

self.scrollView.contentSize=CGSizeMake(self.view.frame.size.width*2, self.view.frame.size.height*2);

如果我像上面那样设置contentSize,它会在第一个下面给出另一个(空白)视图。

我该如何尝试解决方案?

1 个答案:

答案 0 :(得分:0)

这是你要找的吗?此代码将ViewController2和ViewController3添加到第二个滚动视图(一个在另一个的顶部),然后将第二个滚动视图添加到最初添加ViewController2的位置的原始滚动视图。

UIViewController* VC1=[[UIViewController alloc]initWithNibName:@"ViewController1" bundle:nil];
[self addChildViewController:VC1];
[self.scrollView addSubview:VC1.view];
[VC1 didMoveToParentViewController:self];

UIScrollView *scroll = [[UIScrollView alloc] initWithFrame:CGRectMake(self.view.frame.size.width, 0, self.view.frame.size.width, self.view.frame.size.height)];
scroll.contentSize = CGSizeMake(self.view.frame.size.width, self.view.frame.size.height * 2);
[self.scrollView addSubview:scroll];

UIViewController* VC2=[[UIViewController alloc]initWithNibName:@"ViewController2" bundle:nil];
[self addChildViewController:VC2];
[scroll addSubview:VC2.view];
[VC2 didMoveToParentViewController:self];

UIViewController* VC3=[[UIViewController alloc]initWithNibName:@"ViewController3" bundle:nil];
[self addChildViewController:VC3];
[scroll addSubview:VC3.view];
[VC3 didMoveToParentViewController:self];

CGRect VC3frame=VC3.view.frame;
VC3frame.origin.y=self.view.frame.size.height;
VC3.view.frame=VC3frame;

self.scrollView.contentSize=CGSizeMake(self.view.frame.size.width*2, self.view.frame.size.height);