gpt4 book ai didi

ios - 无法在 UIScrollView 中完全滚动

转载 作者:行者123 更新时间:2023-11-29 00:12:57 26 4
gpt4 key购买 nike

我在 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)

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

最佳答案

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

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

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

关于ios - 无法在 UIScrollView 中完全滚动,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45888339/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com