How to do zoom in UIScrollView?

Introduction on Zoom in UIScrollView

UIScrollView is class or we can say its a tool which is used to present UIView and other elements in its contentView. it does not bounded by size of device because its contentView size can be set as per our requirement and due to scrolling feature and zoom functionality. Items like UIView and many mora can be added in UIScrollView by addSubview and then setting of proper constraints.

Zoom in UIScrollView

Overview for Zoom in UIScrollView

Here in section we are going to learn how to make zoom in UIImageView when added in UIScrollView. For this you should know some basic idea about UIScrollView else if you want to know about UIScrollView then check this https://developer.apple.com/documentation/uikit/uiscrollview.

Steps for Creating Zoom in UIScrollView

Step 1. First Add UIScrollView in UIViewController and then proper set constraints of UIScrollView after that add UIImageView in UIScrollView.

Step 2. After this add Proper Constraint to UIImageView according to UIScrollView

Step 3. Create IBOutlets for UIImageView and UIScrollView

IBOutlet UIScrollView * scrollView;
IBOutlet UIImageView * imgView;

Step 4. Add delegates method in your UIViewController.h file “UIScrollViewDelegate”.

Step 5. Now add these code in your viewDidLoad method,

- (void)viewDidLoad {
[super viewDidLoad];
float minScale=scrollView.frame.size.width / imgView.frame.size.width;
bgScrollView.minimumZoomScale = minScale;
bgScrollView.maximumZoomScale = 3.0;
bgScrollView.contentSize = imgView.frame.size;
bgScrollView.delegate = self;
}

Step 6. Add ScrollView’s delegate method for zoom,

- (UIView *)viewForZoomingInScrollView:(UIScrollView *)scrollView
{
return imgView;
}

Leave a Comment