UITableView高度更改时更新其他元素的约束 [英] Update constraints of other elements when UITableView height change

查看:30
本文介绍了UITableView高度更改时更新其他元素的约束的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个 UITableView ,我在自动布局中将高度定义为200.基于此,我将在其下面布置其他元素,例如 UITextField 等.在运行时之后,由于要更新,我从服务器获取数据并填充在 UITableView UITableView 的高度基于其内容大小.以下是我正在使用的代码

I have a UITableView of which I am defining height as 200 in autolayout. Based on that I am laying out other elements below it like UITextField etc. After that in run time I am fetching data from server and populating in UITableView due to which i am updating UITableView's height based on its content size. Following code I am using for it

self.myTableView.frame = CGRectMake(0 , 0, self.myTableView.frame.width, self.myTableView.contentSize.height)

但是,由于这个原因,放置在 UITableView 下面的所有元素仍然显示在与在自动布局"中进行布局时相同的位置.表示 UITableView 的高度变化对它们没有影响.下图描述了此问题.有什么可能的解决方案?

But due to this, all the elements placed below UITableView still appear at same location which they were while laying out in Autolayout. Means change in height of UITableView makes no difference to them. Following image depicts this problem. What could be possible solution for this?

在这里您可以看到,文本字段在运行时在tableview上变得重叠.我正在Xcode 7.2中使用Swift 2

Here you can see, text fields are getting overlapped on tableview at run time. I am using Swift 2 in Xcode 7.2

推荐答案

如果您对表视图和其他视图具有所有必需的约束.

If you have all required constraints to your table view and other view.

请勿更改TableView的框架以更改其高度.而是创建高度为Tablview约束的IBOutlet.

Don't change the frame of TableView to change height of it. Instead create IBOutlet of height constraint of your tablview.

例如假设IBOutlet的名称为constraintTableViewHeight,那么您就可以轻松获得较高的收益.

e.g. say IBOutlet name is constraintTableViewHeight, then you can the the hight easily.

constraintTableViewHeight.constant = yourNewHeightValue
//update all constraint of your view and its inner view
self.view.layoutIfNeeded();

请参考图像为您的身高约束创建IBOutlet.

Refer Image to create IBOutlet for your height Constraint.

这篇关于UITableView高度更改时更新其他元素的约束的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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