UIScrollView lets a user display content that is larger than the device window. You can create a Scroll View either programmatically or using the Interface Builder. Setting up a scroll view is easy, all you have to do a put other views inside the scroll view and set the size of the scroll view using the contentSize property. As long as the size of the scroll views is bigger than the screen window it will scroll. But its always recommended to set the scroll view width or the height same as the window so that scroll happens only in one direction such as horizontally or vertically for better user experience.

Steps :

  1. Create a new Xcode Project for (Single View Application).
  2. Give Project a name ( Lets say SimpleScrollView).
  3. Click next button and the Project will be created.
  4.  Now go to Main StoryBoard of the Project and In the Object Library, select the “ScrollView” object and drag it into the view. Also Select a imageView and drag it into the ScrollView.
  5. Put the  image in the imageView which You want to Scroll.
  6. Connect the ScrollView delegate to the View Controller.
  7. Create a IBoutlet for the ScrollView and ImageView in the ViewContoller.h.

After Connectimg the IBOutlet Your View Controller will Look something Like this.

// ViewController.h

// SimpleScrollView

//

// Created by Nilesh on 4/13/16.

// Copyright © 2016 Nilesh. All rights reserved.

//

#import <UIKit/UIKit.h>

@interface ViewController : UIViewController

@property (weak, nonatomic) IBOutlet UIScrollView *scrollView;

@property (weak, nonatomic) IBOutlet UIImageView *imageView;

 

@end

7. Next go to ViewController.m and all the below Delegate Methods.

# pragma – mark ScrollViewDelegateMethods

-(UIView *)viewForZoomingInScrollView:(UIScrollView *)scrollView{

return self.imageView;

}

8. Then add this custom Method to set the Maximum and Minimum Zoom Scale for the ScrollView and call this Method from the ViewDidLoad.

# pragma – mark custom Methods

-(void)AddScrollView{

self.scrollView.accessibilityActivationPoint = CGPointMake(100, 100);

self.scrollView.minimumZoomScale = 0.5;

self.scrollView.maximumZoomScale = 3;

}

– (void)viewDidLoad {

[super viewDidLoad];

[self AddScrollView];

// Do any additional setup after loading the view, typically from a nib.

}

9. Run the add and zoom the image to see the desired output.. Enjoy Coding 🙂

Advertisements