1
votes

I was asked to help out in a project with swift and work in xcode on a short timescale, neither which I have worked with before.

The task is getting a picture to be "zoomable". And after some researched I found that putting a UIScrollView on top of the image will suffice. Now, since the image view already exists and is integrated in the code on a level I do not dare triffle with, with some constraits and what not. I dont want to start the process all over with a new image view and later try to hook it into the code. Mostly because the image view is inside a table cell inside of a table view.

What I have done is:

On the storyboard

  • Put the image view inside the view on the storyboard.
  • Assigned the delegate of the scroll view to the table cell.
  • I couldnt quite figure out how to constraint these so I mostly use 'Add missing constraints'.

In code for the TableViewCell

  • Inherited the UIScrollViewDelegate
  • made a new var with @IBOutlet weak var scrollView: UIScrollView!
  • Since I am in a controller, i cannot override viewDidLoad function, so I implemented that and set minimum and maximumZoomScale to some values.
  • Implemented a viewForZooming function that returns the UIImageView

I figured out somewhat how I can use the constraints and properties of the ImageView to resize and stuff, but the regardless of what I try to do, I cannot get the "zoom" to work.

Is there any property that the ImageView could have that is messing this up, what should I check for?

1
i would suggest, that you built a custom ScrollView with ImageView in it. with that you could use init/layoutSubviews method to configure the scrollView so it can properly zoom the assigned image (no pixelparty etc.) - Marcel T
since i am fairly new, you mean according to the steps I have taken, or should i use another aproach? - Joel Wahlund
As I understand what you explain in your question you just put an scollView above an Image View. What I mean is, that you Make a class e.g. "ImageScrollView" which is a subclass of UIScrollView, UIScrollViewDelegate which has a UIImageView as variable. AFAIK there is also a Apple Tutorial for that, I will look that up. - Marcel T
Look for ImageScrollView.m in the PhotoScroller Project. It's in obj-c but it shows what to do. - Marcel T
Thanks, got it to work finally by some miracle! :-) - Joel Wahlund

1 Answers

1
votes

After reading and trying to figure out, what finally made it work was this:

  • The ScrollView was made parent in the Interface Builder to the ImageView
  • I magically made the constraints to fit the ScrollView aligned to everything around it mostly by Add missing constraints when prompted with constraint errors.
  • Rightclicked the ScrollView then for the delegate, clicked the + sign and dragged that to the UITableViewCell.
  • I also did the same for the Referencing Outlet
  • And code for the UITableViewCell is this:

    class ClothesItemTableViewCell: UITableViewCell, UIScrollViewDelegate {
      @IBOutlet weak var scrollView: UIScrollView!
      var theImage: UIImage? {
         set {
            clothinhImageView.image = newValue
        } get {
           return clothingImageView.image
        }
      }
    
      func viewForZooming(in scrollView: UIScrollView) -> UIView? {
         return clothingImageView
      }
    }
    

Might not be perfectly clear, and I couldnt find the exact article that made the work done. But there are plenty that tries to solve this issue in different ways, and somehow this worked. Hopes it helpes somebody!