我正在尝试向UIScrollView添加子视图。首先,我从故事板中实例化了视图控制器,然后将视图的框架设置为应用程序边界。当我将视图添加到UIScrollView时,它显然比它应该的要大。
CGRect mainFrame = CGRectMake(0, topButtonHeight, screenWidth, screenHeight);
feelingVC = (FeelingViewController *)[self.storyboard instantiateViewControllerWithIdentifier:@"feelingVC"];
feelingVC.delegate = self;
feelingView = feelingVC.view;
[feelingView setFrame:mainFrame];
[self.scrollView addSubview:feelingView];
我可以看出背景颜色超出了它应该的范围。在XCode的“Debug View Hierarchy”模式下也确认了这一点。但是,如果我检查视图的框架,它打印的是应该是什么,而不是实际的值。
完全以编程方式生成的相同大小的视图按预期工作:
mainView = [[UIView alloc] initWithFrame:mainFrame];
mainView.backgroundColor = [UIColor blackColor];
[self.scrollView addSubview:mainView];
我不确定是什么原因导致了这个问题 - 我已经从它们的视图控制器中实例化了其他视图,同样也通过故事板进行设置他们的框架而没有遇到任何问题。
编辑: 这是从包含UIScrollView的视图控制器的viewDidLoad中调用的。 screenWidth和screenHeight是如下计算的(它们是实例变量):
screenWidth = [UIScreen mainScreen].applicationFrame.size.width;
screenHeight = [UIScreen mainScreen].applicationFrame.size.height;