【发布时间】:2025-12-07 12:45:01
【问题描述】:
在我的应用程序中,我尝试根据内容创建自动调整大小的滚动视图,这是我的意思的简化版本。 我知道我应该使用 contentView,但是这样不起作用!
view.addSubview(mainScrollView)
mainScrollView.addSubview(contentView)
mainScrollView.anchorFor(superview: view) // .zero to all edges of view
let view1 = UIView(frame: view.frame)
view1.backgroundColor = .red
let view2 = UIView(frame: view.frame)
view2.backgroundColor = .brown
view2.frame.origin.y = view1.frame.height
mainScrollView.contentSize.height = 2 * view.frame.height
现在,如果我不指定 contentSize,只会出现红色视图。 我也试过这种方式:
let contentView = UIView(frame: CGRect(x: 0, y: 0, width: self.view.bounds.width, height: self.view.bounds.height))
contentView.translatesAutoresizingMaskIntoConstraints = false
contentView.backgroundColor = .black
contentView.fillSuperview() // .zero to scrollview edges, but it doesn't work
.
.
.
contentView.addSubviews(view1,view2)
如何根据可以更改的 contentView 大小调整自己的 scoll 视图,例如通过单击“展开”按钮,所有这些都以编程方式没有情节提要。 谢谢
【问题讨论】:
标签: swift view uiscrollview