Dynamic UIView height with auto layout in iOS 6(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 高度的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:iOS 6 中具有自动布局的动态 UIView 高度
基础教程推荐
- UIWebView 委托方法 shouldStartLoadWithRequest:在 WKWebView 中等效? 2022-01-01
- 如何让对象对 Cocos2D 中的触摸做出反应? 2022-01-01
- 当从同一个组件调用时,两个 IBAction 触发的顺序是什么? 2022-01-01
- 如何在 UIImageView 中异步加载图像? 2022-01-01
- 在 gmail 中为 ios 应用程序检索朋友的朋友 2022-01-01
- android 应用程序已发布,但在 google play 中找不到 2022-01-01
- 如何在没有IB的情况下将2个按钮添加到右侧的UINavigationbar? 2022-01-01
- Android:对话框关闭而不调用关闭 2022-01-01
- Kivy Buildozer 无法构建 apk,命令失败:./distribute.sh -m “kivy"d 2022-01-01
- 如何在 iPhone 上显示来自 API 的 HTML 文本? 2022-01-01