我有一个包含两个子视图的UIScrollView,每个子视图都与设备屏幕的大小完全相同。我在AppDelegate
:
CGRect screenRect = [[self window] bounds];
[[myViewController1 view] setFrame:screenRect];
screenRect.origin.x = screenRect.size.width;
[[myViewController2 view] setFrame:screenRect];
然后我将这些视图添加到UIScrollView
并将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限制垂直滚动并强制其子项在适当大小的帧中渲染?
答案 0 :(得分:2)
您是否已记录[[自我窗口]界限?它可能包含状态栏,这将使您的内容视图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];