2015-04-03 86 views
0

有人可以向我解释UIScrollView和AutoLayout的工作原理吗?我试图做到以下几点:UIScrollView,Autolayout和webview

UIImageView顶部 下UIImageView有一个标签 在有标签一个UIWebView

我设法找到了我怎么能在网页视图后修改的WebView高度加载(代表和webViewDidFinishLoad),但我无法弄清楚自动布局。

它滚动像魅力,但不能适应屏幕大小,它只发生在UIScrollView,在普通视图中,自动布局与此UI元素没有问题。

http://s10.postimg.org/4vl541e2x/K_perny_fot_2015_04_03_12_52_30.png http://s10.postimg.org/nmn2e78nd/K_perny_fot_2015_04_03_12_53_36.png

回答

0

这可能不是被链接到网页视图,但到了滚动。

基本上,您需要在您的父视图(滚动视图的父级)和可能是Web视图的内容视图(滚动视图的子级)之间手动设置布局约束。

请务必将单个视图添加到滚动视图,而不是多个视图。

然后在视图中手动添加约束,就像这样:

NSLayoutConstraint *leftConstraint = [NSLayoutConstraint constraintWithItem:self.contentView 
                   attribute:NSLayoutAttributeLeading 
                   relatedBy:0 
                   toItem:self.view 
                   attribute:NSLayoutAttributeLeft 
                  multiplier:1.0 
                   constant:0]; 
[self.view addConstraint:leftConstraint]; 

NSLayoutConstraint *rightConstraint = [NSLayoutConstraint constraintWithItem:self.contentView 
                   attribute:NSLayoutAttributeTrailing 
                   relatedBy:0 
                    toItem:self.view 
                   attribute:NSLayoutAttributeRight 
                   multiplier:1.0 
                   constant:0]; 
[self.view addConstraint:rightConstraint]; 

其中内容查看是滚动视图的孩子。

看看这篇文章的详细信息:http://spin.atomicobject.com/2014/03/05/uiscrollview-autolayout-ios/

有人说,它可以通过设置相同类型的约束在生成器是可能的,但我没能做到这一点,而这就像一个魅力。