Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

iOS 7 zooming is not working in ScrollView with AutoLayout but working in iOS8/9

I have made a demo for zoom image using UIScrollView. My ViewController only contains one image. The problem is the image cannot zoom in iO7 (I have tested on iPhone4S-iOS7) but work perfectly in iOS8/iOS9.
Any ideas on how to fix it?

Here is my code

#import "ViewController.h"

@interface ViewController ()<UIScrollViewDelegate>
@property (weak, nonatomic) IBOutlet UIScrollView *scrollview;
@property (weak, nonatomic) IBOutlet UIView *contentview;

@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
        float minimumScale = [_contentview frame].size.width /[_scrollview frame].size.width;
        _scrollview.maximumZoomScale = 5;  //Change as per you need
        _scrollview.minimumZoomScale = minimumScale;  //Change as you need
        _scrollview.zoomScale = minimumScale;
        _scrollview.delegate =self;
        _scrollview.clipsToBounds = YES;
}

- (UIView *)viewForZoomingInScrollView:(UIScrollView *)scrollView{
    return self.contentview;
}

@end

Here is the layout structure

enter image description here

Screen.png constraint enter image description here ContentView constraint
enter image description here
ScrollView constraint enter image description here

Here is my demo project

https://drive.google.com/file/d/0B679aXO0SBmMeUVHTUdOcmxJSXM/view

like image 634
Linh Avatar asked Feb 18 '16 04:02

Linh


1 Answers

The height and width constraints are causing this in iOS 7. A workaround would be, removing those constraints for iOS 7 and calculate minimumScale manually. in IOS 8 and above, do not change anything.

enter image description here

- (void)viewDidLoad {
    [super viewDidLoad];

    float minimumScale = 1;

    if (floor(NSFoundationVersionNumber) < NSFoundationVersionNumber_iOS_8_0) {
        [self.view removeConstraints:self.heightWidthConstraints];
        minimumScale = self.scrollview.frame.size.width / self.imageView.image.size.width;
    }

    _scrollview.maximumZoomScale = 5;  //Change as per you need
    _scrollview.minimumZoomScale = minimumScale;  //Change as you need
    //_scrollview.zoomScale = minimumScale;
    _scrollview.delegate = self;
    //_scrollview.clipsToBounds = YES;

    [self.scrollview setZoomScale:minimumScale animated:YES];
}
like image 176
Warif Akhand Rishi Avatar answered Nov 11 '22 17:11

Warif Akhand Rishi