首页 > 解决方案 > UIView layoutConstraint 奇怪的行为,粘在上面

问题描述

我有一个表视图作为子视图(contentView 是超级视图)。它有一个值为 190 的顶部约束,但在我的 iPhone 6 上它一直保持在顶部(origin.y 为 0.0)。但是,在我的 iPhone X 上,它完全正常。

- (void)adjustHeightOfViews {

    //adjust contentview-height to tableview-height
    self.tableViewTopConstraint.active = YES;
    self.tableViewTopConstraint.constant = 190;

    self.tableViewHeightConstraint.constant = 0.0f;
    for (int i = 0; i < [_cellTitles count]; i ++) {
        self.tableViewHeightConstraint.constant += [self.tableView rectForRowAtIndexPath:[NSIndexPath indexPathForRow:i inSection:0]].size.height;

        }        
        self.contentViewHeightConstraint.constant = self.tableViewHeightConstraint.constant + 500.0f;


    self.tableViewTopConstraint.constant = 190;
}

viewheights 在我的 adjustHeightOfViews 方法中设置,我在 viewDidLayoutSubviews 中调用该方法(我在这里尝试在 viewDidAppear 和其他任何地方调用该方法,但不能解决问题)。

这里有什么问题?

编辑

当我在我的 cellForRow 中禁用 [celllayOutIfNeeded] 时问题就消失了(我当然需要它),太奇怪了!

-> 之后 tableView 有一个正常工作的 topConstraint

标签: iosobjective-cuiviewios-autolayout

解决方案


推荐阅读