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

时间:2016-02-18 04:13:42

标签: ios objective-c uiscrollview autolayout

我使用UIScrollView制作了缩放图像演示。我的ViewController只包含一张图片。 问题是图片无法放大iO7(我已在iPhone4S-iOS7上测试过)但在iOS8 / iOS9中完美运行。
关于如何解决它的任何想法?

这是我的代码

#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

这是布局结构

enter image description here

Screen.png 约束 enter image description here ContentView 约束
enter image description here
ScrollView 约束 enter image description here

这是我的演示项目

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

2 个答案:

答案 0 :(得分:1)

高度和宽度constraints在iOS 7中导致此问题。解决方法是,删除iOS 7的constraints并手动计算minimumScale。在IOS 8及以上版本中,不要更改任何内容。

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

答案 1 :(得分:0)

enter image description here
删除contentView的宽度和高度约束。 使用contentView

添加scrollView的centralHorizo​​ntal约束

让我知道它是否有效。