ScrollView contentSize大于Bounds,但没有滚动

时间:2015-07-25 00:11:42

标签: ios swift uiscrollview frame bounds

我有一个包含单个图像的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左右。

1 个答案:

答案 0 :(得分:0)

正如Muc Dong评论的那样,我正在将图像的宽度添加到scrollView的contentSize.width中。这不一定是正确的事情,因为在我的情况下,我没有考虑的图像之间有一些差距。此余量应包含在scrollView.contentSize.width的附加内容中。