【发布时间】:2013-06-28 16:56:06
【问题描述】:
我正在尝试做这样的事情:
- (void)viewWillAppear:(BOOL)animated
{
[super viewWillAppear:animated];
NSLog(@"%@", self.tableViewHeight);
self.tableViewHeight.constant = 0;
NSLog(@"%@", self.tableViewHeight);
[self.tableView setNeedsUpdateConstraints];
[self.tableView setNeedsLayout];
[self.view setNeedsUpdateConstraints];
[self.view setNeedsLayout];
}
但有时这有效,有时无效。我总是在日志中看到正确的消息:
<NSLayoutConstraint:0x9ebe7a0 V:[UITableView:0xa345a00(304@500)] priority:500>
<NSLayoutConstraint:0x9ebe7a0 V:[UITableView:0xa345a00(0@500)] priority:500>
为什么会这样?如何在 UI 可见之前更改 NSLayoutConstraints 属性?
更新:
这段代码工作了 20 次中的 20 次(但我认为现在总是这样):
- (void)viewWillAppear:(BOOL)animated
{
[super viewWillAppear:animated];
dispatch_async(dispatch_get_main_queue(), ^{
self.tableViewHeight.constant = 0;
[self.view setNeedsUpdateConstraints];
});
}
请告诉我为什么会发生这种情况?
更新 2:
看起来以前的代码仍然无法正常工作,但这是可行的:
- (void)viewWillAppear:(BOOL)animated
{
[super viewWillAppear:animated];
self.topViewHeight.constant += self.tableViewHeight.constant;
self.tableViewHeight.constant = 0;
[self.view setNeedsUpdateConstraints];
}
更新 3:
我永远不会为具有关联视图的多个约束设置相同的优先级(例如 500)...
【问题讨论】:
标签: ios autolayout viewdidload viewwillappear