以编程方式在 ViewController 中添加 UIScrollView,其中包含带有对象的较小 UIView

发布于 2025-01-20 04:05:48 字数 760 浏览 3 评论 0原文

我有一个 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文