【发布时间】:2018-04-08 06:38:06
【问题描述】:
我一整天都在努力让它正常工作。我尝试了不同的 tuto 并进行了调整(在此处搜索)但它无法正常工作。
在 iphone 5s 上,它几乎可以正常工作,除了最后一页似乎在滚动视图上添加了“一半”页面。绿色部分是滚动视图的背景色,不会反弹。
在 iphone 6 上,内容全乱了,不是有 5 张图片,而是只有 4 张半(我不能再滚动了,也不会反弹)。
如果我按继续,它在两种屏幕尺寸上都能完美运行。只有滑动选项不能正常工作。
这是我将图像放入滚动视图的代码。
func loadContent() {
var frame = CGRect(x: 0, y: 0, width: 0, height: 0)
for i in 0..<imageArr.count { // is 5
frame.origin.x = scrollView.frame.size.width * CGFloat(i)
frame.size = self.scrollView.frame.size
let imageView = UIImageView(frame: frame)
let image = UIImage(named: imageArr[i])
imageView.autoresizingMask = [.flexibleWidth, .flexibleHeight, .flexibleBottomMargin, .flexibleRightMargin, .flexibleLeftMargin, .flexibleTopMargin]
imageView.contentMode = .scaleAspectFit
imageView.clipsToBounds = true
imageView.backgroundColor = UIColor.red
imageView.image = image
scrollView.addSubview(imageView)
self.scrollView.contentSize.width = self.scrollView.frame.width * CGFloat(i)
}
}
///Function of Continue button
@IBAction func nextImage(_ sender: Any) {
UIView.animate(withDuration: 0.3) {
self.scrollView.contentOffset.x = self.scrollView.frame.width * CGFloat(self.pageControl.currentPage+1)
}
}
我一直在寻找 4 小时的解决方案,但似乎没有任何效果。
[编辑]:有了TheFuquan的回答,我就可以解决问题了。
[Obs:]如果您搜索混乱的内容,这些代码行可以帮助您。我看到的所有 tutos 都没有这些行,内容无法完美对齐,这解决了:
imageView.autoresizingMask = [.flexibleWidth, .flexibleHeight, .flexibleBottomMargin, .flexibleRightMargin, .flexibleLeftMargin, .flexibleTopMargin]
imageView.contentMode = .scaleAspectFit
imageView.clipsToBounds = true
【问题讨论】:
-
更好的选择是使用带有水平滚动的集合视图。
-
您能说明一下您是如何实例化滚动视图的吗?
-
@StefanStefanov 我正在使用情节提要。我放了一个滚动视图,添加了一些约束(左边距、右边距和底部边距)并在我的代码中引用它。
标签: ios iphone swift uiscrollview