UIScrollView zooming with Auto Layout(UIScrollView 使用自动布局缩放)
问题描述
我正在尝试实现 UIScrollView
新方式,使用自动布局.我已经设置了从内部视图到滚动视图的约束,以便它可以自动计算自己的 contentSize
,这就像一个魅力——除了当我尝试放大时所有的地狱都崩溃了或出去.我什至无法恰当地描述发生了什么,只能说内心的看法搞砸了".
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".
您可以在此处查看此行为的示例(不是我的项目; 您必须设置滚动视图的 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?
推荐答案
我见过的最好的答案是 Mark 的 (https://stackoverflow.com/users/1051919/mark-kryzhanouski),发布在这里:UIScrollView 缩放不适用于自动布局.
The best answer that I have seen is Mark's (https://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 中生成视图层次结构,但您仍然必须以编程方式添加约束.您可以删除部分或全部默认约束(主要只是为了安抚约束冲突警告),但您始终需要 Mark 的代码将图像视图绑定到滚动视图的父级,即图像视图的祖父级.
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 使用自动布局缩放的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:UIScrollView 使用自动布局缩放
基础教程推荐
- 在 gmail 中为 ios 应用程序检索朋友的朋友 2022-01-01
- 如何在没有IB的情况下将2个按钮添加到右侧的UINavigationbar? 2022-01-01
- 如何在 UIImageView 中异步加载图像? 2022-01-01
- android 应用程序已发布,但在 google play 中找不到 2022-01-01
- 当从同一个组件调用时,两个 IBAction 触发的顺序是什么? 2022-01-01
- 如何让对象对 Cocos2D 中的触摸做出反应? 2022-01-01
- Kivy Buildozer 无法构建 apk,命令失败:./distribute.sh -m “kivy"d 2022-01-01
- Android:对话框关闭而不调用关闭 2022-01-01
- UIWebView 委托方法 shouldStartLoadWithRequest:在 WKWebView 中等效? 2022-01-01
- 如何在 iPhone 上显示来自 API 的 HTML 文本? 2022-01-01