iOS 6 中具有自动布局的动态 UIView 高度 [英] Dynamic UIView height with auto layout in iOS 6

查看:24
本文介绍了iOS 6 中具有自动布局的动态 UIView 高度的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在过去的几天里,我试图使用自动布局约束来完成一个相当简单(至少应该如此)的布局,但没有成功.

For the last couple of days I am trying to accomplish a fairly simple (at least it should be) layout using auto layout constraints, but without success.

我的视图层次结构是:
UIScrollView
-- UIView(容器视图)
-----UILabel(多行标签)
-----UIWebView
-----UILabel
-----UI按钮

My view hierarchy is:
UIScrollView
-- UIView (Container view)
-----UILabel (Multirow label)
-----UIWebView
-----UILabel
-----UIButton

所需的功能是根据内容的大小扩展容器视图.为了增加 UIWebView 的高度,我使用以下代码:

The desired functionallity is the Container view to be expanding according to the size of the contents. In order to increase the height of the UIWebView I use the following code:

- (void)webViewDidFinishLoad:(UIWebView *)webView {
    [self.webView sizeToFit];
} 

我尝试了许多不同的限制条件,其中最合理的限制条件是:
1. 将超级视图的顶部空间固定为第一个标签(事件标题)
2. 固定第一个标签和 UIWebView 的垂直间距
3. 为其余元素固定垂直间距(即 UIWebView - UILabel (Event Date) 和 UILabel (Event Date) - UIButton)
4.容器视图优先级低(1)底部垂直空间约束与其父视图

I tried many different constraints, with the most reasonable ones being:
1. Pin top space from superview for the 1st label (Event Title)
2. Pin vertical spacing for the 1st label and the UIWebView
3. Pin vertical spacing for the rest elements (i.e. UIWebView - UILabel (Event Date) and UILabel (Event Date) - UIButton)
4. The container view has low priority (1) bottom vertical space constraint with its superview

我得到的结果如下:

UIWebView 展开但不下推事件日期标签和按钮,另外 Container 视图也不展开.

The UIWebView expands but does not push down the event date label and the button, plus the Container view does not expand either.

任何帮助将不胜感激.

您可以从此处下载示例 Xcode 项目.

推荐答案

您将 webView 的高度约束设置为 266.这就是为什么 web view 的高度仍然是固定的.

You are setting the height contraint of your webView as 266. That's why the height of the web view is still fixed.

您可以将此高度约束创建为 IBOutlet,例如:

You can create this height constraint as an IBOutlet, for example:

@property (weak, nonatomic) IBOutlet NSLayoutConstraint *webViewHeightConstraint;

然后你可以在网页视图下载完内容后修改高度约束的常量.Web 视图本身由内部的滚动视图组成,因此如果您想获取内容的整体高度:

And then you can modify the constant of the height constraint when the web view has finished downloading the content. The web view itself consists of scroll view inside, so if you want to get the overall height of the content:

- (void)webViewDidFinishLoad:(UIWebView *)webView {
    self.webViewHeightConstraint.constant = self.webView.scrollView.contentSize.height;
}

或者显然这个也可以:

- (void)webViewDidFinishLoad:(UIWebView *)webView {
    [self.webView sizeToFit];
    self.webViewHeightConstraint.constant = self.webView.frame.size.height;
}

这篇关于iOS 6 中具有自动布局的动态 UIView 高度的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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