我有两个 View Controller ,我想将其添加为 ScrollView 的 subview ,但是,当我添加这些 View Controller 时,仅显示第二个 Controller 。
这是我的代码。
- (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];
}
提前致谢。
最佳答案
给你举个例子,你可以根据你的要求调整他们的框架。
- (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];
}
关于ios - 在 ScrollView 中添加现有的 View Controller ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25254823/