简体   繁体   English

ScrollView contentSize比界限大,但没有滚动

[英]ScrollView contentSize Larger than Bounds, Yet No Scrolling

I have a scrollView containing a single image. 我有一个包含单个图像的scrollView。 The image's bounds are greater than the scrollView's frame. 图像的边界大于scrollView的框架。 I have set the scrollView's contentSize to be equal to the image's bounds. 我已经将scrollView的contentSize设置为等于图像的边界。 Yet no scrolling. 但是没有滚动。 I have no idea what could be causing this. 我不知道是什么原因造成的。

 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

    }

}

I've printed out the scrollView.contentSize.width and the scrollView.bounds.width and the content width is in fact greater than the bounds width by about 40. 我已经打印出scrollView.contentSize.width和scrollView.bounds.width,内容宽度实际上比边界宽度大了约40。

As Muc Dong commented, I was adding the width of the images to the scrollView's contentSize.width. 正如Muc Dong所说,我正在将图像的宽度添加到scrollView的contentSize.width中。 This was not necessarily the correct thing to do as in my case there is some margin between the images that I was not accounting for. 这不一定是正确的做法,因为在我的情况下,我没有考虑的图像之间有一定的余量。 This margin should of been included in the additions to to scrollView.contentSize.width. 此边距应包含在scrollView.contentSize.width的附加内容中。

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

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