以编程方式在 ViewController 中添加 UIScrollView,其中包含带有对象的较小 UIView
我有一个 UIViewcontroller。我在这个 UIVIewController 中有一个较小的 UIVIew。现在我试图以编程方式添加一个 UIScrollView ,它应该包含较小的 UIView ,这样我就可以上下滚动较小的视图。较小的 UIView 已使用 xcode 的界面生成器创建。所以现在我使用以下代码添加scrollView,然后在scrollView中添加较小的View。滚动视图的宽度应与较小的视图相同。
self.scrollView = [[UIScrollView alloc] initWithFrame:CGRectMake(self.smallerView.frame.origin.x, self.smallerView.frame.origin.y, self.smallerView.frame.size.width, self.smallerView.frame.size.height)];
[self.scrollView setContentSize:CGSizeMake(self.smallerView.bounds.size.width, self.smallerView.bounds.size.height*1.2)];
[self.scrollView addSubview:self.smallerView];
[self.view addSubview:self.scrollView];
奇怪的是,较小的 UIView 中包含的对象不在 UIScrollView 的边界内。我缺少什么?我是否必须以编程方式为较小的 UIView 添加约束?任何帮助表示赞赏。
I have an UIViewcontroller. I have a smaller UIVIew in this UIVIewController. Now i am trying to add an UIScrollView programmatically that should contain the smaller UIView so i can scroll the smaller view up and down. The smaller UIView is already created using the interface builder of xcode. So now i use the following code to add the scrollView and then add the smaller View in the scrollView. The scrollView should have the same size in width as the smaller View.
self.scrollView = [[UIScrollView alloc] initWithFrame:CGRectMake(self.smallerView.frame.origin.x, self.smallerView.frame.origin.y, self.smallerView.frame.size.width, self.smallerView.frame.size.height)];
[self.scrollView setContentSize:CGSizeMake(self.smallerView.bounds.size.width, self.smallerView.bounds.size.height*1.2)];
[self.scrollView addSubview:self.smallerView];
[self.view addSubview:self.scrollView];
The weird thing is that the objects that are contained in the smaller UIView are not inside the bounds of the UIScrollView. What am i missing? Do i have to add constraints for the smaller UIView programmatically? Any help appreciated.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论