gpt4 book ai didi

swift - 将 ScrollView 添加到 Collection View 层次结构

转载 作者:搜寻专家 更新时间:2023-11-01 05:41:39 24 4
gpt4 key购买 nike

我有一个 View Controller ,其中包含许多 subview 、建立的所有连接等。我发现我现在需要一个 ScrollView 以允许在较小的屏幕尺寸上显示所有内容。

我的问题是如何将 ScrollView 添加到 View 层次结构的顶部并允许滚动所有 View ?

提前致谢

最佳答案

您需要先创建包含所有 subview 的 UIScrollView。然后您可以遍历所有顶级 subview 并将它们从它们的父 View 移到 ScrollView 中。

假设在您的 UIViewController 中:

// You will need to handle auto-resize, frame adjustment, and auto-layout stuff
let scrollView = UIScrollView(frame: self.view.frame)

for view in self.view.subviews
{
// Move all subviews from the main view to the scroll view
for subView in self.view.subviews as [UIView]
{
subView.removeFromSuperview()
scrollView.addSubview(subView)
}
}

// Insert the scroll view as the first subview
self.view.insertSubview(scrollView, atIndex: 0)

如前所述,您必须处理这些 View 的布局(自动调整大小的 mask 、约束等)。

关于swift - 将 ScrollView 添加到 Collection View 层次结构,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29110513/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com