更改表视图的高度约束viewDidAppear之外iOS中7 [英] Change height constraint of table view outside of viewDidAppear in iOS 7

查看:168
本文介绍了更改表视图的高度约束viewDidAppear之外iOS中7的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的视图控制器的.xib文件,我使用的是的UITableView 里面有行的动态数。因为它不是在文件的.xib的唯一元素(下面有与上面的其它元素),我需要改变它的高度约束之后,我知道的行数。
要做到这一点,我使用的<一个href=\"http://stackoverflow.com/questions/14223931/change-uitableview-height-dynamically#answer-14228015\">this解决方案工作正常。结果
因此:

In my View Controller's .xib file I'm using a UITableView which has a dynamic number of rows. Because it's not the only element in the .xib file (there are other elements below and above it), I need to change it's height constraint after I know the number of rows. To achieve this, I'm using this solution that works fine.
Therefore:

@implementation MyViewController
{
__weak IBOutlet UITableView *_tableView;
__weak IBOutlet NSLayoutConstraint *_tableViewHeightConstraint;
}

- (void)viewDidAppear:(BOOL)animated
{
 [super viewDidAppear:animated];
 [self adjustHeightOfTableview];
}

- (void)adjustHeightOfTableview
{
 CGFloat height = _tableView.contentSize.height;
_tableViewHeightConstraint.constant = height;
[self.view needsUpdateConstraints];
}

现在这个工作得很好,但问题是,你可以看到,当被显示(因为当然在 viewDidAppear 这样做)的的ViewController的高度变化。< BR>
我想避免这种情况,并正在显示的ViewController时,应直接与所需的高度显示。结果
我已经打过电话 adjustHeightOfTableview viewDidLayoutSubviews 而不是 viewDidAppear 作为建议<一href=\"http://stackoverflow.com/questions/20930000/change-height-constraint-programmatically\">here,但是,让我这个错误:

Now this works fine, but the problem is that you can see the height changing when the ViewController is being shown (because of doing this in viewDidAppearof course).
I would like to avoid this and when the ViewController is being shown, it should be shown directly with the needed height.
I've tried calling adjustHeightOfTableview in viewDidLayoutSubviews instead of viewDidAppear as suggested here, but that gives me this error:

*** Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: 'Auto Layout still required after sending -viewDidLayoutSubviews to the view controller. MyViewController's implementation needs to send -layoutSubviews to the view to invoke auto layout.'

我注意到,如果我叫 [self.view layoutSubviews]; 我的 adjustHeightOfTableview 方法的末尾,然后,我没有得到任何错误,但是我的表视图是不可见的。检查 _tableView.contentSize.height viewDidAppear 调试器告诉我,它有正确的值。 (根据我的行数)

I noticed that if I call [self.view layoutSubviews]; at the end of my adjustHeightOfTableview method, then I don't get any error, but my table view is not visible at all. Checking _tableView.contentSize.height with the debugger in viewDidAppear show me that it has the right value. (according to my number of rows)

调用 [self.view layoutIfNeeded]; [self.view setNeedsLayout]; 在结束我的 adjustHeightOfTableview 方法具有相同的行为与调用 [self.view layoutSubviews]; :没有错误,但我的表视图不可见。

Calling [self.view layoutIfNeeded]; or [self.view setNeedsLayout]; at the end of my adjustHeightOfTableview method has the same behavior as calling [self.view layoutSubviews]; : no error, but my table view is not visible.

在的情况下它的事项,我使用自定义的的UITableViewCell

In case it matters, I'm using a custom UITableViewCell.

有谁知道我在做什么错了?

Does anybody know what am I doing wrong?

推荐答案

viewDidAppear移动code: viewWillAppear中:。他们是大同小异,并且有效的几乎相同的各种各样的事情,但 viewWillAppear中:发生了用户的目光,而这正是你追求的。 (您可能需要调用 reloadData 在桌子上首次明确,为了给它一些数据。)

Move the code from viewDidAppear: to viewWillAppear:. They are much the same, and valid for much the same sorts of thing, but viewWillAppear: happens out of the user's gaze, which is exactly what you're after. (You'll probably need to call reloadData on the table explicitly first, in order to give it some data.)

这篇关于更改表视图的高度约束viewDidAppear之外iOS中7的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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