用的UIScrollView自动布局缩放 [英] UIScrollView zooming with Auto Layout

查看:184
本文介绍了用的UIScrollView自动布局缩放的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想实现一个的UIScrollView 中的新途径,使用自动布局。我从内以滚动视图,以便它可以计算自己的 contentSize 自动,这就像一个charm-除了所有的地狱破散设置限制当我尝试放大或缩小。我甚至不能正确地描述发生了什么,比其他地说,内观被搞砸了。

I'm trying to implement a UIScrollView the New Way, using Auto Layout. I've set up constraints from the inner view to the scroll view so that it can compute its own contentSize automatically, and that works like a charm— except that all hell breaks loose when I try to zoom in or out. I can't even properly describe what happens, other than to say that the inner view gets "messed up".

您可以看到此行为的示例<一个href=\"https://github.com/mattneub/Programming-iOS-Book-Examples/tree/master/ch20p573scrollViewAutoLayout2\">here (不是我的项目,你必须设置滚动视图的 maximumZoomScale 和实施 -viewForZoomingInScrollView:之前缩放将工作)。

You can see an example of this behavior here (not my project; you have to set the scroll view's maximumZoomScale and implement -viewForZoomingInScrollView: before zooming will work).

有没有其他人碰到这种行为?目前有什么办法得到一个放大的UIScrollView 基本上没有自己重新实现缩放行为与自动布局工作?

Has anyone else run into this behavior? Is there currently any way to get zooming in a UIScrollView to work with Auto Layout without essentially re-implementing the zooming behavior yourself?

推荐答案

这是我所见过的最好的答案是马克的(<一个href=\"http://stackoverflow.com/users/1051919/mark-kryzhanouski\">http://stackoverflow.com/users/1051919/mark-kryzhanouski),张贴在这里:<一href=\"http://stackoverflow.com/questions/14433109/uiscrollview-zoom-does-not-work-with-autolayout\">UIScrollView变焦不工作,自动布局。

The best answer that I have seen is Mark's (http://stackoverflow.com/users/1051919/mark-kryzhanouski), posted here: UIScrollView Zoom Does Not Work With Autolayout.

问题的症结所在是,你必须锚定嵌套在滚动视图,滚动视图的父级的图像显示。尽管在iOS 6中发行说明中的​​指导,这不是直观的对我有什么看法是浮动过一下。在这种情况下,滚动视图仅仅是一个单一的图像图。

The crux of it is that you have to anchor the image view that is nested in the scroll view, to the parent of the scroll view. Despite the guidance in the iOS 6 release notes, it is not intuitive to me what view is "floating" over what. In this case, the scrolling view is just a single image view.

我做了很多与此实验,希望找到一个全IB方法,并没有发现。您仍然可以生成IB视图层次,但你还是以编程方式添加约束。您可以删除某些或所有的默认约束(主要只是为了安抚约束冲突的警告),但你总是需要马克的code到图像视图配合滚动视图的父,的的祖父母图像视图。

I did do a lot of experimentation with this, hoping to find an all-IB approach and found none. You can still generate the view hierarchy in IB, but you still have to programatically add constraints. You can delete some or all of the default constraints (mainly just to appease the constraint-conflict warnings), but you always need Mark's code to tie the image view to the parent of the scroll view, the grand-parent of the image view.

现在看来似乎应该比这个更简单 - 它应该只是工作,而是:

It seems like it should be simpler than this - it "should just work" but:

NSDictionary *viewsDictionary = @{ @"scrollView": self.scrollView, @"imageView": self.imageView };
[self.view addConstraints:[NSLayoutConstraint
    constraintsWithVisualFormat:@"H:|[imageView(width)]"
    options:0
    metrics:@{@"width": @(self.imageView.image.size.width)}
    views:viewsDictionary]];

[self.view addConstraints:[NSLayoutConstraint
    constraintsWithVisualFormat:@"V:|[imageView(height)]"
    options:0
    metrics:@{@"height": @(self.imageView.image.size.height)}
    views:viewsDictionary]];

这篇关于用的UIScrollView自动布局缩放的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆