I've been stuck on this for hours. All I want is an ImageView in a ScrollView. I want the contentMode to be set to aspect fill so the image fills the ImageView every time. I also want the user to be able to zoom and pan around the image. Here is my code:
import UIKit
class ViewController: UIViewController, UIScrollViewDelegate {
var scrollView: UIScrollView!
var imageView: UIImageView!
override func viewDidLoad() {
super.viewDidLoad()
let image = UIImage(named: "fish")!
imageView = UIImageView(image: image)
imageView.frame = CGRect(origin: CGPoint(x: 0, y: 0), size:image.size)
imageView.contentMode = .scaleAspectFill
scrollView = UIScrollView(frame: view.bounds)
scrollView.backgroundColor = UIColor.black
scrollView.contentSize = imageView.bounds.size
scrollView.delegate = self
scrollView.minimumZoomScale = 0.1
scrollView.maximumZoomScale = 4.0
scrollView.zoomScale = 1.0
scrollView.addSubview(imageView)
view.addSubview(scrollView)
}
func viewForZoomingInScrollView(scrollView: UIScrollView) -> UIView? {
return imageView
}
}
First of all I can't zoom in or out, don't understand why. I set the delegate, implemented viewForZoomingInScrollView and set the maximumZoomScale from what I read that is required for zooming to work.
Secondly the image does not Aspect Fit the screen. The content mode setting does not seem to affect anything because when I set it to .scaleToFill the image is not squeezed into the screen and I can pan around it.
Image smaller than the screen: http://i.imgur.com/eEuy0En.png
Original: http://i.imgur.com/GtkKWKu.jpg
Image larger than the screen: http://i.imgur.com/1cH4rrk.png
Original: http://i.imgur.com/ThNhapb.jpg
The smaller image should aspect fill the screen but it doesn't and and the zooming does not work in both cases. I can only pan around the larger image.