1
votes

I have created a VC with orientation in Landscape in storyboard

I have added an UIIScrollView in it , say, make it: (w)1000, 500 (h) in the VC.

What I wanted to do:

1) Scrolling the image (with high resolution like 1334 x 750) inside ScrollView
2) view the image in ScrollView in landscape mode


To make ScrollView to display the image, I have to do it in `viewDidAppear` 

but here the Problems:

1) The Width and height of the `ScrollView` is gone
2) label on top gone.
3) The `ScrollView` Size become small something like 200 x 150 and start from the Top corner like (0,0)
What I need to do to make `scrollview` size like before 1000 x 500? --- Update --
class ViewController: UIViewController, UIScrollViewDelegate { @IBOutlet weak var myUIScrollView: UIScrollView! var imgView: UIImageView! override func viewDidLoad() { super.viewDidLoad() //-- force to landscape mode: let value = UIInterfaceOrientation.LandscapeLeft.rawValue UIDevice.currentDevice().setValue(value, forKey: "orientation") self.myUIScrollView.maximumZoomScale = 10.0 self.myUIScrollView.minimumZoomScale = 1.0 self.myUIScrollView.delegate = self imgView = UIImageView(image: UIImage(named: "MyPhoto.png")) } override func viewDidAppear(animated: Bool) { self.myUIScrollView.contentSize = imgView.bounds.size self.myUIScrollView.addSubview(imgView) view.addSubview(myUIScollView) } override func shouldAutorotate() -> Bool { return true } func viewForZoomingInScrollView(scrollView: UIScrollView) -> UIView? { return imgView }
1
If you are using Xcode assets and you have an image collection named my-image, you have to to use UIImage(name: "my-image").matthias
Not working. Say I created image set in Xcasset call : Photo1 and my put MyPhoto.png ,MyPhoto1.png , MyPhoto2.png ,..in it. I followed your method. I use UIImage(named: "Photo1"), it wont work.MilkBottle
You have to create an image set for each image and the spaces are for three sizes like image1.png, [email protected] and [email protected] and then you can init your UIImage with name "image1".matthias
Displaying image is ok now after I changed the code in viewDidAppear. The problem now is that the Scrollview become so small in my updated code. it stick to the top corner.MilkBottle

1 Answers

0
votes

Try this:

import UIKit

class ViewController: UIViewController, UIScrollViewDelegate {

var image: UIImage!{
    get{
        return myImageView.image!
    }set{
        myImageView.image = newValue
        myImageView.sizeToFit()
        myScrollView.contentSize = myImageView.frame.size
    }
}

var myImageView = UIImageView()

@IBOutlet weak var myScrollView: UIScrollView!{
    didSet{
        myScrollView.delegate = self
        myScrollView.minimumZoomScale = 10.0
        myScrollView.maximumZoomScale = 1.0
        myScrollView.addSubview(myImageView)
    }
}

override func viewDidLoad() {
    super.viewDidLoad()
    // Do any additional setup after loading the view, typically from a nib.
    image = UIImage(named: "one")
}

func viewForZooming(in scrollView: UIScrollView) -> UIView? {
    return myImageView
}
}