无法在 UIScrollView 中完全滚动

Can't fully scroll within UIScrollView

我在 UIScrollView 中有一个 UIImageView,我希望能够滚动 "regular" 照片尺寸。我的问题是我可以滚动,但是当它到达照片的顶部或底部时,滚动位置不会停留,而是 "bounces" 并向上移动一点点,并且不会允许照片留在那个位置,我将如何解决这个问题?

下面是代码:

 scrollView.frame = CGRect(x: self.view.frame.width * 0, y: self.view.frame.height / 3, width: self.view.frame.width, height: self.view.frame.width)
        self.view.addSubview(scrollView)
        scrollView.delegate = self


        let image = imageView.image
        imageView.frame = CGRect(x: self.view.frame.width * 0, y: self.view.frame.height * 0, width: (image?.size.width)!, height: (image?.size.height)!)
        imageView.frame = scrollView.bounds
        imageView.contentMode = .scaleAspectFill
        scrollView.addSubview(imageView)

举个例子,如果您用相机拍了一张全屏照片,然后当照片显示在视图中时,整张照片无法在滚动时保持原样。

弹跳是 UIScrollView(及其子类,包括 UiTableView)中的可选行为。您可以通过 bounces property 或在 Interface Builder 中将其变为 on/off。

我通常以一种方便的方法(或者您可以使用 UIAppearance)将所需的 ScrollView 属性捆绑在一起,以确保我的所有视图都具有共享行为。

您做错了很多事情,如前所述,您应该转向自动布局和约束,但是...

要让您的图像按照您的方式滚动:

    // set scroll view frame to be full width of view, start 1/3 of the way down from the top, and make the height the same as the width
    scrollView.frame = CGRect(x: 0, y: self.view.frame.height / 3, width: self.view.frame.width, height: self.view.frame.width)

    // add the scroll view to the view
    self.view.addSubview(scrollView)

    // use "if let" so you're not force-unwrapping optionals
    if let image = imageView.image {

        // set the imageView's frame to the size of the image
        imageView.frame = CGRect(x: 0, y: 0, width: image.size.width, height: image.size.height)

        // add the imageView to the scroll view
        scrollView.addSubview(imageView)

        // set the contentSize of the scroll view - the "scrollable area" - to the size of the image view
        scrollView.contentSize = imageView.bounds.size
    }