0

I have an image inside of a UIScrollView. The Image's length is much larger then the device screen size and that is why i placed it in a scrollview. However, to avoid clipping and distortion of the image I have the constraints set so the image will always have the correct aspect ratio. When you run this on different size screens, the image is different sizes. Now I want to be able to have the scrollview be the same size as my image so that the scrollview is never bigger then the image itself. Is there some code I can program in to make my scrollview always the same height as my image? I need my scrollview to change depending on the height of the image since on different devices the height of my image will change.

so far I have:

class Second : UIViewController {

@IBOutlet weak var myScrollView: UIScrollView!
@IBOutlet weak var myImage: UIImageView!

override func viewDidLoad() {

    ScrollView.contentSize.height = 1300

}

The constraints I used on the image are:

  • Equal width as the scrollview
  • entered horizontally in the scrollview
  • Top space to the top of the scrollview
  • Aspect fit (so the image is not distorted)

Thank you. Any help is very appreciated!

rmaddy
  • 314,917
  • 42
  • 532
  • 579
Lucas Azzopardi
  • 1,131
  • 2
  • 11
  • 21
  • Help me understand this better, why would you need a scroll view in the first place if you always want the width/height of the scroll view to be the same to that of the image? – Gurtej Singh Aug 01 '15 at 18:50
  • I need a scrollview because my image is larger then the screen so I need to be able to scroll through the image – Lucas Azzopardi Aug 01 '15 at 18:55
  • ok and what are your constraints on the scroll view if any, and what is it's frame otherwise? I am asking because you have set the width of the image same as the scroll view. – Gurtej Singh Aug 01 '15 at 18:58
  • The only constraints I have set in the scroll view are: equal heights and widths to the main view controller and to centre it horizontally and vertically in that controller. Then in my code I set the height of it to allow the user to scroll. I have attempted to alter the constraints of the scrollview to be the same as the images heights and all that but it prevents me from scrolling. I used equal width so that the width of the image would fill the entire screen – Lucas Azzopardi Aug 01 '15 at 19:02
  • Also, how are you loading your image? URL? Image asset? – Gurtej Singh Aug 01 '15 at 19:08
  • I am loading it through the image assets – Lucas Azzopardi Aug 01 '15 at 19:09
  • Thanks for the info, please see my answer below and let me know if it works! I'll research more if it doesn't. Cheers. – Gurtej Singh Aug 01 '15 at 19:11
  • Just like this and many others: http://stackoverflow.com/questions/9346331/proper-contentsize-of-uiscrollview-with-uiimageview-inside-it – danh Aug 01 '15 at 19:15
  • I think that's incorrect @danh. He will not get the frame of the image in the viewDidLoad method. He is using auto-layout. – Gurtej Singh Aug 01 '15 at 19:20
  • @GurtejSingh - the questioner is incorrect, yes. and the answer ought to point that out. your suggestion of using viewDidLayout... is a good one. the point is, it's 90% a dup. – danh Aug 01 '15 at 19:21
  • Yup @danh. The question seems to be asked many times, its the auto-layout part which makes it interesting and different I guess for me. And you are right, I hope my answer points that out. Thanks! – Gurtej Singh Aug 01 '15 at 19:26
  • No problem :) Request you to please accept the answer (Click the Tick mark below the voting buttons on the left of my answer). Not completely a dup for me :). Glad I could help. Thanks. – Gurtej Singh Aug 01 '15 at 19:42

3 Answers3

0

You can set the UIScrollView contentSize property in your viewDidLoad like the following way:

override func viewDidLoad() {
   myScrollView.contentSize = CGSize(width: widthYouWantToSet, height: heightYouWantToSet)
}

But, be careful in you can experience rotations in your app, this need to be updated if the height change. I hope this help you.

Victor Sigler
  • 23,243
  • 14
  • 88
  • 105
  • I don't think that is the answer he is looking for. He wants to know the height (frame) of the image at run time, which would be dynamic since he is using auto-layout constraints on the image. And then, he wants to set his scroll view of the same size as the image. Please see if you can help him answering that. – Gurtej Singh Aug 01 '15 at 18:55
0

Try this, override your 'viewDidLayoutSubviews' method and you shall be able to get the frame of the image within that method (you will not get the frame of the image in your viewDidLoad method, see reference link below). From the frame, get the height of the image, and then set the content size for the scrollview as pointed by Victor above. Let me know if it works.

See this question for reference: iOS AutoLayout - get frame size width

Community
  • 1
  • 1
Gurtej Singh
  • 3,244
  • 1
  • 14
  • 27
0

Should be,

override func viewDidLoad() {

    myScrollView.contentSize.height = 1300

}
Sazzad Hissain Khan
  • 37,929
  • 33
  • 189
  • 256