我正在尝试做这样的事情:
- (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)......