1

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 }
4
  • If you are using Xcode assets and you have an image collection named my-image, you have to to use UIImage(name: "my-image"). Commented Jun 3, 2015 at 10:15
  • 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. Commented Jun 3, 2015 at 10:38
  • 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". Commented Jun 3, 2015 at 15:03
  • 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. Commented Jun 4, 2015 at 4:43

1 Answer 1

0

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
}
}
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.