ScrollView contentSize 大于边界,但没有滚动
ScrollView contentSize Larger than Bounds, Yet No Scrolling
我有一个包含单个图像的滚动视图。图像的边界大于滚动视图的框架。我已将 scrollView 的 contentSize 设置为等于图像的边界。
但是没有滚动。我不知道是什么原因造成的。
override func viewDidLoad() {
super.viewDidLoad()
var matches = SortThread.getSortThread().retrieveMatches()
scrollView.frame = CGRectMake(0, navigationController!.navigationBar.frame.height - 20, UIScreen.mainScreen().bounds.width, view.bounds.height - (navigationController!.navigationBar.frame.height - 20))
var imageName = String()
imageName = matches[0].pictures![0]
var parsedImageName = imageName.componentsSeparatedByString(".") as [String]
var newImageName: String = parsedImageName[0] as String
let path = NSBundle.mainBundle().pathForResource("MVLMP Images (Resized)/" + newImageName, ofType: "jpg")
var image = UIImage()
image = UIImage(contentsOfFile: path!)!
var imageView = UIImageView(image: image)
var ratio: CGFloat = image.size.width/image.size.height
var screenHeight: CGFloat = UIScreen.mainScreen().bounds.height - navigationController!.navigationBar.frame.height - 20
if(ratio > 1){
imageView.frame = CGRectMake(0, 0, ((scrollView.bounds.width)/7) * 6, 0)
imageView.frame = CGRectMake(0, 0, imageView.frame.width, imageView.frame.width * ratio)
} else {
imageView.frame = CGRectMake(0, 0, 0, (screenHeight/9)*8)
imageView.frame = CGRectMake(0, 0, imageView.frame.height * ratio, imageView.frame.height)
}
scrollView.addSubview(imageView)
scrollView.contentSize.width = imageView.frame.width
scrollView.contentSize.height = imageView.frame.height
}
}
我已经打印出 scrollView.contentSize.width 和 scrollView.bounds.width,内容宽度实际上比边界宽度大大约 40。
正如 Muc Dong 评论的那样,我将图像的宽度添加到 scrollView 的 contentSize.width。这不一定是正确的做法,因为在我的情况下,图像之间存在一些我没有考虑的空白。这个边距应该包含在对 scrollView.contentSize.width 的添加中。
我有一个包含单个图像的滚动视图。图像的边界大于滚动视图的框架。我已将 scrollView 的 contentSize 设置为等于图像的边界。 但是没有滚动。我不知道是什么原因造成的。
override func viewDidLoad() {
super.viewDidLoad()
var matches = SortThread.getSortThread().retrieveMatches()
scrollView.frame = CGRectMake(0, navigationController!.navigationBar.frame.height - 20, UIScreen.mainScreen().bounds.width, view.bounds.height - (navigationController!.navigationBar.frame.height - 20))
var imageName = String()
imageName = matches[0].pictures![0]
var parsedImageName = imageName.componentsSeparatedByString(".") as [String]
var newImageName: String = parsedImageName[0] as String
let path = NSBundle.mainBundle().pathForResource("MVLMP Images (Resized)/" + newImageName, ofType: "jpg")
var image = UIImage()
image = UIImage(contentsOfFile: path!)!
var imageView = UIImageView(image: image)
var ratio: CGFloat = image.size.width/image.size.height
var screenHeight: CGFloat = UIScreen.mainScreen().bounds.height - navigationController!.navigationBar.frame.height - 20
if(ratio > 1){
imageView.frame = CGRectMake(0, 0, ((scrollView.bounds.width)/7) * 6, 0)
imageView.frame = CGRectMake(0, 0, imageView.frame.width, imageView.frame.width * ratio)
} else {
imageView.frame = CGRectMake(0, 0, 0, (screenHeight/9)*8)
imageView.frame = CGRectMake(0, 0, imageView.frame.height * ratio, imageView.frame.height)
}
scrollView.addSubview(imageView)
scrollView.contentSize.width = imageView.frame.width
scrollView.contentSize.height = imageView.frame.height
}
}
我已经打印出 scrollView.contentSize.width 和 scrollView.bounds.width,内容宽度实际上比边界宽度大大约 40。
正如 Muc Dong 评论的那样,我将图像的宽度添加到 scrollView 的 contentSize.width。这不一定是正确的做法,因为在我的情况下,图像之间存在一些我没有考虑的空白。这个边距应该包含在对 scrollView.contentSize.width 的添加中。