問題描述
我正在嘗試實現 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 使用自動布局縮放的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!