ios - Swift - UIImageView zooms bigger than UIScrollView -
i'm having problem pinch zooming uiimageview
set inside uiscrollview
. image zooms in response user's pinch inputs, image zooms , gets bigger until takes entire screen rather staying inside bounds of uiscrollview
. have had @ heap of different questions, articles , youtube videos try , find source of problem haven't been able find anything.
the following questions seem loosely related still couldn't resolve problem after looking @ them:
- content size of uiscrollview
- uiscrollview scaled uiimageview using autolayout
- uiscrollview squashes uiimageview image
- swift uiimageview stretched aspect
- uiimageview pinch zoom swift
my uiscrollview
created in storyboard , used layout constraints created in interface builder anchor in place. set scroll view's delegate self in viewdidload()
(and have added uiscrollviewdelegate
top of class). uiimageview
created programmatically. declare image view @ top of class below:
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
i add image view sub view of uiscrollview
follow line of code, called during viewdidload()
:
self.scrollview.addsubview(self.imageview)
after adding image view scroll view, load image image view. fetch image url, , load in using following code. again, called in viewdidload()
.
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() }
this confirms image has loaded correctly, hides warning label display if hadn't, , calls self.formatimageviewerwithimage()
, below:
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 }
i have implemented scrollviewdidzoom
with:
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 }
and viewforzooming
with:
func viewforzooming(in scrollview: uiscrollview) -> uiview? { return self.imageview }
i have seen previous questions (including listed above) suggested adding code viewwilllayoutsubviews()
, have not had success that, viewwilllayoutsubviews()
empty.
can see doing wrong?
please change line:
self.scrollview.clipstobounds = false
to line:
self.scrollview.clipstobounds = true
hope helps!
Comments
Post a Comment