All Projects → evgenyneu → Ios Imagescroll Swift

evgenyneu / Ios Imagescroll Swift

iOS demo of using image view inside a scroll view with auto layout in Swift

Programming Languages

swift
15916 projects

Image in a scroll view with auto layout in iOS

This is a iOS demo app that shows how to use an image in a scroll view with auto layout. It is written in Swift. Image can be panned and zoomed. It is centered and zoomed out initially. Adapts to device orientation change with animation.

Using image inside a scroll view with auto layout in iOS 7

Things to note

  • The job is done with auto layout constraints only.
  • On each zoom we update the constant values for the constraints to center the image if needed.
  • Feel free to contact me if you are stuck (like I was).

How to copy this control to your project

  • In your storyboard 'Use Auto Layout' setting must enabled.
  • Copy ViewController.swift file into your project.
  • Copy the View Controller into your storyboard, like this:
Copy Image View Controller

Troubleshooting

Here are things to check if something goes wrong.

  • Your view controller's class is ViewController in Identity Inspector.
  • Storyboard has all the controls: View, ScrollView and ImageView along with all their constraints.
  • To enable zooming ScrollView's 'max zoom' should be greater than 1 (Attributes Inspector). I used 3 in this demo.
  • ImageView, ScrollView, four layout constraints and 'change image' button should be linked to their outlets in ViewController.swift.
  • If you have a navigation bar clear the Adjust Scroll View Insets checkbox in the Attribute Inspector of your view controller.
Clear "Adjust Scroll View Insets" in your View Controller.
  • Set the Intrinsic size property to Placeholder in the Size inspector of the image view.
Set the intrinsic size to placeholder for the image view.

Image constraint item order

For each of the four auto layout constraints between the image and the scroll view: first item must be image (and not superview).

image constraint item order

Objective-C version

This repository replaces previous objective-c version.

Thanks

Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].