2

我有一个 UIScrollView 包含两个子视图,每个子视图都是设备屏幕的大小。我在我的设置子视图的大小AppDelegate

CGRect screenRect = [[self window] bounds];
[[myViewController1 view] setFrame:screenRect];
screenRect.origin.x = screenRect.size.width;
[[myViewController2 view] setFrame:screenRect];

然后我将这些视图添加到 aUIScrollView并将 UIScrollView 设置contentSize为屏幕宽度的两倍:

CGRect containerRect = CGRectMake(0, 0, screenRect.size.width * 2, screenRect.size.height);
UIScrollView *sv = (UIScrollView *)[myRootViewController view];
[sv addSubview:[myViewController1 view]];
[sv addSubview:[myViewController2 view]];
[sv setFrame:[[self window] bounds]];
[sv setContentSize:containerRect.size];

当应用程序运行时,我可以看到并排的视图,正如预期的那样。但是,两个视图都比屏幕宽度宽得多。此外,UIScrollView不限制垂直滚动,我希望它的高度等于屏幕高度。

如何使 UIScrollView 限制垂直滚动并强制其子级在适当大小的框架中呈现?

4

1 回答 1

2

您是否记录了您的 [[self window] bounds]?它可能包含状态栏,这会使您的内容视图 20pts 太高(从而启用垂直滚动)。也许:

CGRect screenFrame = [[UIScreen mainScreen] applicationFrame];
myViewController1.view.frame = CGRectMake(0.0, 0.0, CGRectGetWidth(screenFrame), CGRectGetHeight(screenFrame));
myViewController2.view.frame = CGRectOffset(myViewController1.view.frame, CGRectGetWidth(screenFrame), 0.0);

接着:

CGRect contentRect = CGRectMake(0, 0, CGRectGetWidth(screenFrame), CGRectGetHeight(screenFrame));
UIScrollView *sv = (UIScrollView *)myRootViewController.view;
[sv addSubview:myViewController1.view];
[sv addSubview:myViewController2.view];
[sv setContentSize:contentRect.size];
于 2013-08-05T20:09:12.967 回答