匹配子视图的宽度是使用上海华自动版式 [英] Matching subview's width to it's superview using autolayout

查看:116
本文介绍了匹配子视图的宽度是使用上海华自动版式的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

目标:

有一个的UIWebView 是相同的宽度,因为它是上海华,这是一个的UIScrollView ,使用自动布局约束。

Have a UIWebView be the same width as it's superview, which is a UIScrollView, using autolayout constraints.

code

NSLayoutConstraint *makeWidthTheSameAsScrollView =[NSLayoutConstraint
                                                       constraintWithItem:self.questionWebView
                                                       attribute:NSLayoutAttributeWidth
                                                       relatedBy:0
                                                       toItem:self.masterScrollView
                                                       attribute:NSLayoutAttributeWidth
                                                       multiplier:1.0
                                                       constant:0];

[self.view addConstraint:makeWidthTheSameAsScrollView];

 NSLog(@"The width of questionWebView *AFTER* adding the constrain is: %f", self.questionWebView.frame.size.width);
 NSLog(@"The width of scrollView *AFTER* adding the constrain is: %f", self.masterScrollView.frame.size.width);

当前结果

当我登录 self.questionWebView 的宽度(的UIWebView ),它的宽度不会改变的时候自动布局约束被应用。

When I log the width of self.questionWebView (the UIWebView), it's width does not change when the autolayout constrain is applied.

问题


  • 这是正确的做法?

  • 我在做什么错了?

PS我知道这是对苹果的建议,将的UIWebView 的UIScrollView ,但是我已经变成关闭滚动的能力的UIWebView 使用属性 self.questionWebView.userInteractionEnabled = NO; 。而目前使用一个UIWebView是用来显示一个HTML表我最好的策略。

p.s I know it is against Apple's recommendations to place a UIWebView in a UIScrollView, however I've turned off the ability to scroll the UIWebView using the property self.questionWebView.userInteractionEnabled = NO;. And currently using a UIWebView is my best strategy for displaying an HTML table.

推荐答案

在提高罗布的回答,根据要求。

Improving on Rob's answer, as requested.

由于罗布已经提到的, UIScrollViews 有特殊的行为下,自动布局。

As Rob already mentioned, UIScrollViews have peculiar behavior under Auto Layout.

这是在这种情况下,感兴趣的是,滚动视图总宽度是通过使用它的子视图总宽度确定的事实。因此,尽管滚动视图已经要求web视图的宽度,你告诉web视图也要求滚动视图的宽度。这就是为什么它不工作。一个是问另一个,没有人知道答案。你需要另一个参考视图作为web视图约束来使用,然后滚动视图也将能够成功地询问其预期的宽度。

What is of interest in this case is the fact that the scrollView total width is determined by using its subviews total width. So while the scrollView already asks the webView for its width, you're telling the webView to also ask the scrollView for its width. That's why it doesn't work. One is asking another, and no one knows the answer. You need another reference view to use as a constraint for the webView, and then the scrollView will also be able to successfully ask about its expected width.

一个简单的方法可以这样做:创造另一种观点认为, containerView ,并添加滚动视图作为一个子视图来表示。然后设置为 containerView 适当的限制。比方说,你想围绕一个的viewController的滚动视图,对边缘一些填充。所以做了 containerView

An easy way this could be done: create another view, containerView, and add the scrollView as a subview to that. Then set the proper constraints for containerView. Let's say you wanted the scrollView centered on a viewController, with some padding on the edges. So do it for the containerView:

NSDictionary *dict = NSDictionaryOfVariableBindings(containerView);
[self.view addConstraints:[NSLayoutConstraints constraintsWithVisualFormat:@"H|-(100)-[containerView]-(100)-|" options:0 metrics:0 views:dict];
[self.view addConstraints:[NSLayoutConstraints constraintsWithVisualFormat:@"V|-(100)-[containerView]-(100)-|" options:0 metrics:0 views:dict];

然后就可以继续添加web视图作为一个子视图的滚动视图并设置其宽度:

Then you can proceed adding the webView as a subview to the scrollView and setting its width:

NSLayoutConstraint *makeWidthTheSameAsScrollView =[NSLayoutConstraint
                                                       constraintWithItem:self.questionWebView
                                                       attribute:NSLayoutAttributeWidth
                                                       relatedBy:0
                                                       toItem:containerView
                                                       attribute:NSLayoutAttributeWidth
                                                       multiplier:1.0
                                                       constant:0];

[self.view addConstraint:makeWidthTheSameAsScrollView];

这将使滚动视图为高大的作为web视图,并预期他们都将被放置(与containerView设定的约束)。

This would make the scrollview as large and tall as the webView, and they both would be placed as intended (with the constraints set on containerView).

这篇关于匹配子视图的宽度是使用上海华自动版式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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