我想添加一个子视图到UIScrollView。首先,我从故事板实例化视图控制器,然后将视图的框架设置为应用程序边界。当我将视图添加到UIScrollView时,它显然大于它应该是的。UIView的框架渲染大于框架
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调用的。屏幕宽度& screenHeight被计算为这样的(它们是实例变量):
screenWidth = [UIScreen mainScreen].applicationFrame.size.width;
screenHeight = [UIScreen mainScreen].applicationFrame.size.height;
什么功能呢这段代码是从哪里调用的? screenWidth和screenHeight是如何定义的? –
使用scrollView在viewDidLoad中调用视图控制器。 – jdelman
这可能与您的问题相关,也可能不相关,但viewDidLoad通常是设置屏幕/大小相关属性的不可思议的地方,尤其是在使用自动布局的情况下。 viewDidLayoutSubviews通常更好。 –