ios - ScrollView contentSize 大于边界,但没有滚动

标签 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 左右。

最佳答案

正如 Muc Dong 所说,我将图像的宽度添加到 scrollView 的 contentSize.width。这不一定是正确的做法,因为在我的情况下,图像之间存在一些我没有考虑的空白。这个边距应该包含在对 scrollView.contentSize.width 的补充中。

关于ios - ScrollView contentSize 大于边界,但没有滚动,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31621517/

相关文章:

swift - 在 Swift2 中从 "willSelectRowAtIndexPath"的单元格中获取数据

iphone - 如何获得 UITableView 的可见矩形?

iPhone:在设置导航栏显示/隐藏动画时无法设置 contentInset 动画

iphone - 从 TableView 委托(delegate)之外的 UITableView 中删除行

swift - 对于 vDSP_fft_zropD,打包结果似乎是错误的。这是一个错误还是我做错了什么

自定义工具栏中按钮的快速定位(谷歌设计 Material )

ios - UINavigationBar 为 UIScrollView 注册触摸

ios - 更改 anchor ,然后使用 CGAffineTransformMakeRotation 进行旋转

javascript - jQuery Mobile 按钮在按下时不会立即更改为 'pressed' 状态

ios - 在 UIButton 激活后设置延迟