[英]ScrollView contentSize Larger than Bounds, Yet No Scrolling
我有一個包含單個圖像的scrollView。 圖像的邊界大於scrollView的框架。 我已經將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的附加內容中。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.