uitableview 和 uiview 的约束自动布局 [英] Constraints autolayout for uitableview and uiview

查看:34
本文介绍了uitableview 和 uiview 的约束自动布局的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我面临一个约束自动布局问题,即我在上方有一个 UITableView,在下方有一个 UIView.当 UITableView 展开 tableView 部分时,我希望底部的 UIView 自动向下移动,当折叠 tableView 部分时,底部的 UIView 会自动向上移动.

下面是我的故事板设计,绿色部分是 UITableView,红色部分是 UIView.

control + 拖放到视图控制器.你会得到这样的财产.

@property (strong, nonatomic) IBOutlet NSLayoutConstraint * tableviewHeightConstraints;

然后在您切换 tableview 时调用此方法.contentSize 将计算 tableview 高度并将高度更新为 tableviewHeightConstraints 约束.

CGFloat height = MIN(self.view.bounds.size.height, self.tableView.contentSize.height);self.tableviewHeightConstraints.constant = 高度;[self.view layoutIfNeeded];

I'm facing a constraints autolayout issue which is I have a UITableView at upper and UIView at lower. When the UITableView expand the tableView section, I want the UIView at the bottom shift down automatically, when collapse the tableView section, the UIView at bottom will move up automatically.

Below is my storyboard design, on the green sector is UITableView, on the red sector is UIView.

You can get my code from here

Your help is much appreciated.

解决方案

Add the IBOutlet of height constraints of your tableview.

control + drag and drop to the view controller. You will get the property like this.

@property (strong, nonatomic) IBOutlet NSLayoutConstraint * tableviewHeightConstraints;

Then call this one whenever you toggle the tableview. contentSize will calculate the tableview height and update the height to the tableviewHeightConstraints constraints.

CGFloat height = MIN(self.view.bounds.size.height, self.tableView.contentSize.height);
self.tableviewHeightConstraints.constant = height;
[self.view layoutIfNeeded];

这篇关于uitableview 和 uiview 的约束自动布局的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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