Swift - UIImageView缩放比UIScrollView大

时间:2017-08-15 08:19:07

标签: ios swift uiscrollview uiimageview

我在缩放缩放UIImageView时遇到问题,UIScrollView设置在UIScrollView内。图像会根据用户的捏合输入进行缩放,但图像会变大并变大,直到占据整个屏幕而不是保持在UIScrollView的范围内。我已经看了一堆不同的SO问题,文章和YouTube视频,试图找到问题的根源但是却找不到任何东西。

以下SO问题似乎与松散相关,但在查看之后我仍然无法解决我的问题:

在我的故事板中创建了我的viewDidLoad(),并使用在界面构建器中创建的布局约束来将其锚定到位。我在UIScrollViewDelegate中将滚动视图的委托设置为self(并将UIImageView添加到我的班级顶部)。 class VCImageViewer: UIViewController, UIScrollViewDelegate { @IBOutlet weak var lblImageTitle: UILabel! @IBOutlet weak var btnBack: UIButton! @IBOutlet weak var scrollView: UIScrollView! var imageView:UIImageView = UIImageView() // <-- Image view 是以编程方式创建的。我将图像视图声明在类的顶部,如下所示:

UIScrollView

然后我将图片视图添加为viewDidLoad()的子视图,其中包含以下代码行,在self.scrollView.addSubview(self.imageView) 期间调用:

viewDidLoad()

将图像视图添加到滚动视图后,我将图像加载到图像视图中。我从URL获取图像,并使用以下代码加载它。同样,这在self.imageView.image = UIImage(data: data) self.imageView.frame = CGRect(origin: CGPoint(x: 0, y: 0), size: (self.imageView.image?.size)!) if self.imageView.image != nil { self.lblError.isHidden = true self.FormatImageViewerWithImage() } 中被调用。

self.FormatImageViewerWithImage()

这确认图像已正确加载,如果没有,则会隐藏我会显示的警告标签,并调用func FormatImageViewerWithImage() { // Constraints let constraintImageViewTop:NSLayoutConstraint = NSLayoutConstraint(item: self.imageView, attribute: NSLayoutAttribute.top, relatedBy: NSLayoutRelation.equal, toItem: self.scrollView, attribute: NSLayoutAttribute.top, multiplier: 1, constant: 0) let constraintImageViewBottom:NSLayoutConstraint = NSLayoutConstraint(item: self.imageView, attribute: NSLayoutAttribute.bottom, relatedBy: NSLayoutRelation.equal, toItem: self.scrollView, attribute: NSLayoutAttribute.bottom, multiplier: 1, constant: 0) let constraintImageViewLeft:NSLayoutConstraint = NSLayoutConstraint(item: self.imageView, attribute: NSLayoutAttribute.left, relatedBy: NSLayoutRelation.equal, toItem: self.scrollView, attribute: NSLayoutAttribute.left, multiplier: 1, constant: 0) let constraintImageViewRight:NSLayoutConstraint = NSLayoutConstraint(item: self.imageView, attribute: NSLayoutAttribute.right, relatedBy: NSLayoutRelation.equal, toItem: self.scrollView, attribute: NSLayoutAttribute.right, multiplier: 1, constant: 0) self.view.addConstraints([constraintImageViewTop, constraintImageViewBottom, constraintImageViewLeft, constraintImageViewRight]) self.scrollView.contentSize = self.imageView.image!.size self.scrollView.clipsToBounds = false let scrollViewFrame = scrollView.frame let scaleWidth = scrollViewFrame.size.width / self.scrollView.contentSize.width let scaleHeight = scrollViewFrame.size.height / self.scrollView.contentSize.height let minScale = min(scaleWidth, scaleHeight) self.scrollView.minimumZoomScale = minScale self.scrollView.maximumZoomScale = 1.0 self.scrollView.zoomScale = minScale } ,如下所示:

scrollViewDidZoom

我还实施了func scrollViewDidZoom(_ scrollView: UIScrollView) { self.CentreScrollViewContents() } func CentreScrollViewContents() { let boundsSize = self.scrollView.bounds.size var contentsFrame = self.imageView.frame if contentsFrame.size.width < boundsSize.width { contentsFrame.origin.x = (boundsSize.width - contentsFrame.size.width) / 2 } else { contentsFrame.origin.x = 0 } if contentsFrame.size.height < boundsSize.height { contentsFrame.origin.y = (boundsSize.height - contentsFrame.size.height) / 2 } else { contentsFrame.origin.y = 0 } self.imageView.frame = contentsFrame }

viewForZooming

func viewForZooming(in scrollView: UIScrollView) -> UIView? { return self.imageView }

viewWillLayoutSubviews()

我之前的问题(包括上面列出的一些问题)建议将代码添加到viewWillLayoutSubviews(),但我没有取得任何成功,因此我的ref为空。

任何人都能看到我做错的事吗?

1 个答案:

答案 0 :(得分:1)

请更改此行:

self.scrollView.clipsToBounds = false

到这一行:

self.scrollView.clipsToBounds = true

希望这有帮助!