首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >不能在UIScrollView中完全滚动

不能在UIScrollView中完全滚动
EN

Stack Overflow用户
提问于 2017-08-25 19:34:49
回答 2查看 499关注 0票数 0

我有一个UIImageView在一个UIScrollView,我希望能够滚动到“常规”照片维度的维度。我的问题是,我可以滚动,但是当它到达照片的顶部或底部时,滚动的位置不会停留,相反,它会“反弹”,移动一点点,不允许照片停留在那个位置,我该如何修复这个问题呢?

以下代码如下:

代码语言:javascript
复制
 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)

举个例子,如果你用相机拍摄了一张全屏照片,然后当照片在视图中显示时,当它被滚动时,整个照片就不能停留在它的位置上。

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2017-08-25 20:04:20

您做了许多错误的事情,正如前面提到的,您应该移动到自动布局和约束,但是.

为了让你的图像像你要做的那样滚动:

代码语言:javascript
复制
    // 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
    }
票数 1
EN

Stack Overflow用户

发布于 2017-08-25 19:44:32

弹出是UIScrollView (及其子类,包括UiTableView)中的一种可选行为。您可以通过弹跳财产或Interface打开/关闭它。

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

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/45888339

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档