Coder Social home page Coder Social logo

ios-imagescroll-swift's Introduction

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.

Tested in iOS 7, 8 and 9.

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.

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

ios-imagescroll-swift's People

Contributors

evgenyneu avatar

Watchers

 avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.