UIScrollView使用自动布局进行缩放

phu*_*phu 32 zoom uiscrollview ios autolayout

我想实现UIScrollView新途径,利用自动布局.我已经设置了从内部视图到滚动视图的约束,以便它可以contentSize自动计算它自己,并且就像魅力一样 - 除了当我尝试放大或缩小时所有的地狱都松散了.我甚至无法正确描述发生的事情,除了说内部视图"搞砸了".

你可以在这里看到这种行为的一个例子(不是我的项目;你必须设置滚动视图maximumZoomScale-viewForZoomingInScrollView:在缩放工作之前实现).

有没有其他人遇到这种行为?目前是否有任何方法可以放大UIScrollView使用自动布局而无需自己重新实现缩放行为?

AMa*_*yes 3

如果没有在故事板中添加 imageView,我发现以下效果完美:

-(UIImageView *)imageView
{
    if (!_imageView) _imageView = [[UIImageView alloc] initWithFrame:CGRectZero];
    return _imageView;
}
- (void)viewDidLoad
{
    [super viewDidLoad];
    [self.scrollView addSubview:self.imageView];
    // Set the min and max:
    self.scrollView.minimumZoomScale = 0.2;
    self.scrollView.maximumZoomScale = 5.0;
    self.scrollView.delegate = self;

    // Set the content:
    self.scrollView.zoomScale = 1.0; // reset zoomScale for new image
    self.scrollView.contentSize = CGSizeMake(image.size.width/2, image.size.height/2);
    self.imageView.frame = CGRectMake(0, 0, image.size.width/2, image.size.height/2);
    self.imageView.image = image;
}

-(UIView *)viewForZoomingInScrollView:(UIScrollView *)scrollView
{
    return self.imageView;
}
Run Code Online (Sandbox Code Playgroud)

  • @AMayes 因为也许他需要使用自动布局并且需要一个与之兼容的解决方案? (7认同)
  • 您正在手动设置滚动视图的“contentSize”,而不是使用“自动布局”约束,这就是问题的重点。请参阅我发布的第一个链接,了解我的意思的描述(搜索短语“纯自动布局方法”)。 (5认同)