【发布时间】:2021-09-12 14:41:44
【问题描述】:
我一直试图让滚动视图滚动,只是滚动视图应该显示的程度。但是,我做不到。这是我的代码。
func setupMainView() {
// This is where the image view and other UIViews which are supposed to go in the contentview are set up
self.setupImagesView()
self.setupView1()
self.setupView2()
self.setupView3()
self.setupView4()
self.scrollView = UIScrollView()
self.scrollView.backgroundColor = UIColor.white
self.view.addSubview(self.scrollView)
self.automaticallyAdjustsScrollViewInsets = false
self.scrollView.contentInset = UIEdgeInsets.zero
self.scrollView.scrollIndicatorInsets = UIEdgeInsets.zero;
self.contentView = UIView()
self.scrollView.layer.masksToBounds = false
self.scrollView.backgroundColor = UIColor.white
self.scrollView.layer.borderWidth = 0
self.scrollView.layer.borderColor = UIColor.white.cgColor
self.view.addSubview(scrollView)
self.contentView.addSubview(imagesScrollView)
self.contentView.addSubview(view1)
self.contentView.addSubview(view2)
self.contentView.addSubview(view3)
self.contentView.addSubview(view4)
self.scrollView.addSubview(contentView)
self.scrollView.contentInset = UIEdgeInsets.zero
self.scrollView.scrollIndicatorInsets = UIEdgeInsets.zero;
var scrollViewHeight:CGFloat = 0.0;
for _ in self.scrollView.subviews {
scrollViewHeight += view.frame.size.height
}
var newHeight = scrollViewHeight * 1.1 + offset + 100
scrollView.contentSize = CGSize(width:screenWidth, height:newHeight)
scrollView.reloadInputViews()
}
视图正在加载等,但我不管理滚动。不知何故,要么太少,要么太多。
现在,我尝试将 contentSize 的高度设置为 scrollViewHeight 并将其设置为两倍等。我注意到无法预测它将滚动多少。从 1.1 更改为 1.6 .. 视图下方的白屏太多,将其更改为 1.1 或 1.2 它甚至不会滚动到底部。
注意,一切都是以编程方式设置的,没有情节提要等。 另请注意,我需要支持所有版本 > 10 的 IOS 设备。
我有点迷路了。我做错了什么?
【问题讨论】:
-
如果我没记错,那么您将在 self.viiew
self.view.addSubview(scrollView)中添加两次滚动视图。请检查 -
不错,但这似乎不是问题所在。尝试注释掉任何一行,但仍然无法正常工作。
-
您没有使用 AutoLayout 的任何特殊原因?
-
我没有使用 AutoLayout。会试一试,如果这不起作用。为什么这不起作用?
标签: ios swift uiscrollview