【问题标题】:Can't fully scroll within UIScrollView无法在 UIScrollView 内完全滚动
【发布时间】:2018-02-03 21:55:37
【问题描述】:

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

下面是代码:

 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)

例如,如果您使用相机拍摄了一张全屏照片,然后当照片显示在视图中时,整张照片在滚动时无法保持在其位置。

【问题讨论】:

  • 只是一个建议,现在是 swift 3 。使用框架来设置视图不是一个好的选择。

标签: ios swift3 uiscrollview uiimageview contentmode


【解决方案1】:

弹跳是 UIScrollView(及其子类,包括 UiTableView)中的可选行为。您可以通过bounces property 或在 Interface Builder 中打开/关闭它。

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

【讨论】:

    【解决方案2】:

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

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

        // 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
        }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2019-05-05
      • 2016-06-12
      • 2018-03-30
      • 2021-03-23
      • 1970-01-01
      • 2021-12-30
      • 2011-03-26
      • 1970-01-01
      相关资源
      最近更新 更多