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

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

问题描述

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

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结果
-----的UIButton

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

所需functionallity是容器视图根据内容的大小来扩大。为了提高我用下面的code中的一个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(活动日期)和的UILabel(活动日期) - 的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

结果我得到的是以下几点:

The result I get is the following:

该UIWebView的扩展,但不下推事件的日期标签和按钮,加上集装箱观点并不要么扩大。

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

任何帮助将大大AP preciated。

Any help would be greatly appreciated.

您可以从这里下载样本X code项目

You may download the sample Xcode project from here.

推荐答案

您要设置的网页视图的高度约束实现为266.这就是为什么Web视图的高度仍然是固定的。

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视图已完成下载内容可以修改常​​量的高度约束。 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;
}

或明显这一块也可以工作:

Or apparently this one also works:

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

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

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