問(wèn)題描述
我正在嘗試實(shí)現(xiàn) UIScrollView
新方式,使用自動(dòng)布局.我已經(jīng)設(shè)置了從內(nèi)部視圖到滾動(dòng)視圖的約束,以便它可以自動(dòng)計(jì)算自己的 contentSize
,這就像一個(gè)魅力——除了當(dāng)我嘗試放大時(shí)所有的地獄都崩潰了或出去.我什至無(wú)法恰當(dāng)?shù)孛枋霭l(fā)生了什么,只能說(shuō)內(nèi)心的看法搞砸了".
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".
您可以在此處查看此行為的示例(不是我的項(xiàng)目; 您必須設(shè)置滾動(dòng)視圖的 maximumZoomScale
并實(shí)現(xiàn) -viewForZoomingInScrollView:
才能進(jìn)行縮放).
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).
還有其他人遇到過(guò)這種行為嗎?目前有什么方法可以放大 UIScrollView
以使用自動(dòng)布局,而無(wú)需自己重新實(shí)現(xiàn)縮放行為?
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?
推薦答案
我見過(guò)的最好的答案是 Mark 的 (https://stackoverflow.com/users/1051919/mark-kryzhanouski),發(fā)布在這里:UIScrollView 縮放不適用于自動(dòng)布局.
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.
關(guān)鍵是你必須將嵌套在滾動(dòng)視圖中的圖像視圖錨定到滾動(dòng)視圖的父級(jí).盡管 iOS 6 發(fā)行說(shuō)明中提供了指導(dǎo),但對(duì)我來(lái)說(shuō),哪個(gè)視圖浮動(dòng)"于什么之上并不直觀.在這種情況下,滾動(dòng)視圖只是一個(gè)圖像視圖.
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.
我確實(shí)對(duì)此做了很多實(shí)驗(yàn),希望找到一種全 IB 的方法,但沒有找到.您仍然可以在 IB 中生成視圖層次結(jié)構(gòu),但您仍然必須以編程方式添加約束.您可以刪除部分或全部默認(rèn)約束(主要只是為了安撫約束沖突警告),但您始終需要 Mark 的代碼將圖像視圖綁定到滾動(dòng)視圖的父級(jí),即圖像視圖的祖父級(jí).
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.
似乎它應(yīng)該比這更簡(jiǎn)單 - 它應(yīng)該可以工作"但是:
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]];
這篇關(guān)于UIScrollView 使用自動(dòng)布局縮放的文章就介紹到這了,希望我們推薦的答案對(duì)大家有所幫助,也希望大家多多支持html5模板網(wǎng)!