【发布时间】:2016-10-21 19:27:24
【问题描述】:
我整个上午都在与这个问题作斗争,似乎找不到解决办法。我创建了一个UIImageView,用红色填充它,然后将其添加到UIScrollView,并将contentSize 设置为UIImageView 的大小。如果我打印contentOffset,我看到(0, 0),如果我打印contentSize 和UIImageView.frame.size,它们是相同的,但红色的“图像”总是比scrollView 认为的contentSize 小.
如果我一直滚动到顶部,我会在红色图像上方看到大约 100 像素高的青色条纹,并且滚动条不会一直到我认为滚动视图顶部的顶部是。尽管滚动条的顶部确实与我的红色窗口的顶部对齐,所以看起来滚动视图对它实际所在的位置感到困惑。或者更有可能,我很困惑
这是我看起来非常简单的代码...
imgHorizon = UIImage.init(named:"horizon")!
imgBezel = UIImage.init(named:"bezel_transparent")!
imgWings = UIImage.init(named:"wings_transparent")!
imgViewHorizon = UIImageView.init()
imgViewBezel = UIImageView.init()
imgViewWings = UIImageView.init()
svHorizon = UIScrollView.init()
super.init(coder: aDecoder)
imgViewHorizon = UIImageView(frame: CGRect(x: 0, y: 0, width: imgBezel.size.width, height: imgHorizon.size.height))
imgViewHorizon.backgroundColor = UIColor.red
imgViewBezel = UIImageView(frame: CGRect(x: 0, y: 0, width: imgBezel.size.width, height: imgBezel.size.height))
imgViewBezel.contentMode = UIViewContentMode.center
imgViewBezel.clipsToBounds = true
imgViewBezel.image = imgBezel
imgViewWings = UIImageView(frame: CGRect(x: 0, y: 0, width: imgBezel.size.width, height: imgBezel.size.height))
imgViewWings.contentMode = UIViewContentMode.center
imgViewWings.clipsToBounds = true
imgViewWings.image = imgWings
svHorizon = UIScrollView(frame: CGRect(x: 0, y: 0, width: imgBezel.size.width, height: imgBezel.size.width))
svHorizon.contentSize = CGSize(width: imgBezel.size.width, height: imgHorizon.size.height)
svHorizon.contentMode = UIViewContentMode.scaleToFill
svHorizon.bounces = false
svHorizon.backgroundColor = UIColor.cyan
svHorizon.contentOffset = CGPoint(x: 0, y: 0)
svHorizon.addSubview(imgViewHorizon)
addSubview(svHorizon)
addSubview(imgViewBezel)
addSubview(imgViewWings)
【问题讨论】:
-
如需快速诊断,请告知您在
ViewController中的何处编写了此代码?在ViewController的Attributes Inspector中取消选中Adjust Scroll View Insets。 -
我写在一个继承自 UIView 的类中。此代码在
required init?(coder aDecoder: NSCoder)方法中。在界面生成器中,我在视图控制器上放置了一个 UIView 并将类设置为我的自定义类 -
覆盖
layoutSubviews方法并在那里设置imageViews的框架。你有没有取消选中Adjust Scroll View Insets? -
@Adeel 我所做的只是检查 Adjust Scroll View Insets 并开始表现。我会研究你建议的另一件事。昨晚刚开始学习这些东西。
-
您的意思是当您取消选中
Adjust Scroll View Insets选项时问题就消失了?如果是,那么我会将其作为答案发布,因为许多其他人也会从中受益。
标签: ios swift uiscrollview uiimageview