简体   繁体   English

UIScrollView中的子视图放错了位置

[英]Misplaced subviews in UIScrollView

I'm adding a few subviews to a UIScrollView and setting its contentSize, like this: 我正在向UIScrollView添加一些子视图并设置其contentSize,如下所示:

let cellWidth: CGFloat = 100.0
let cellHeight: CGFloat = 100.0

for i in 0...10 {
    let view: UIView = UIView(frame: CGRectMake(CGFloat(i) * cellWidth,  0, cellWidth, cellHeight))
    view.backgroundColor = UIColor.randomColor()
    self.scrollView.addSubview(view)
}
self.scrollView.contentSize = CGSizeMake(10 * cellWidth, cellHeight)

This is what I get: 这是我得到的:

在此处输入图片说明

I don't really understand why these subviews aren't drawn at y=0 我真的不明白为什么这些子视图没有在y = 0处绘制

There are no constraints applied in the storyboard to this UIScrollView. 情节提要中没有对此UIScrollView施加任何约束。

I have found the reason for this. 我已经找到了原因。

For anyone with the same problem, just go to interface builder and select the troubling UIViewController. 对于有相同问题的任何人,只需转到界面生成器并选择麻烦的UIViewController。

In the 'Attributes Inspector' section, on the right pane, untick 'Adjust Scroll View Insets'. 在“属性检查器”部分的右窗格中,取消选中“调整滚动视图插图”。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM