简体   繁体   English

iOS 7缩放在使用AutoLayout的ScrollView中无效,但在iOS8 / 9中有效

[英]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 . 我使用UIScrollView制作了缩放图像演示。 My ViewController only contains one image. 我的ViewController只包含一个图像。 The problem is the image cannot zoom in iO7 (I have tested on iPhone4S-iOS7 ) but work perfectly in iOS8 / iOS9 . 问题是图像无法放大iO7 (我在iPhone4S-iOS7上测试过)但在iOS8 / iOS9完美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 这是布局结构

在此输入图像描述

Screen.png constraint Screen.png约束 在此输入图像描述 ContentView constraint ContentView约束
在此输入图像描述
ScrollView constraint ScrollView约束 在此输入图像描述

Here is my demo project 这是我的演示项目

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

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. 高度和宽度constraints在iOS 7中引起这种情况。解决方法是,删除iOS 7的constraints并手动计算minimumScale in IOS 8 and above, do not change anything. 在IOS 8及以上版本中,不要更改任何内容。

在此输入图像描述

- (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];
}

在此输入图像描述
Remove width and height contraint of contentView . 删除contentView宽度和高度contentView Add centralHorizontal constraint of contentView with scrollView 使用scrollView添加contentView centralHorizo​​ntal约束

Let me know if it works. 如果有效,请告诉我。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM