【问题标题】:Animating constraints is breaking other constraints动画约束打破了其他约束
【发布时间】:2015-03-01 09:27:46
【问题描述】:

我的视图底部有以下表格视图

它有一个高度约束(优先级 250)和一个对视图底部的约束(优先级 1000)。高度约束指向我的视图控制器中的“IBOutlet”。

我想把table view的高度从44.0f改成7*44.0f,所以我做的就是这个;

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (self.categoriesShown) {
        [self hideCategories];
    } else {
        [self showCategories];
    }

    self.categoriesShown = !self.categoriesShown;
}

- (void)showCategories
{
    self.categoriesHeightConstraint.constant = self.categories.count * 44.0f;
}

- (void)hideCategories
{
    self.categoriesHeightConstraint.constant = 44.0f;
}

它工作正常。但是当我尝试使用以下代码为所有这些设置动画时:

- (void)showCategories
{
    [self.categoryTableView layoutIfNeeded];

    [UIView transitionWithView:self.categoryTableView duration:0.3f options:UIViewAnimationOptionCurveEaseOut animations:^{

        self.categoriesHeightConstraint.constant = self.categories.count * 44.0f;
        [self.categoryTableView layoutIfNeeded];

    } completion:nil];
}

- (void)hideCategories
{
    [self.categoryTableView layoutIfNeeded];

    [UIView transitionWithView:self.categoryTableView duration:0.3f options:UIViewAnimationOptionCurveEaseOut animations:^{

        self.categoriesHeightConstraint.constant = 44.0f;
        [self.categoryTableView layoutIfNeeded];

    } completion:nil];
}

然后表格视图和视图底部之间的约束以某种方式被打破,这就是我显示然后隐藏表格视图时得到的结果

有谁知道为什么,约束被打破,但只有当我尝试为更改设置动画时?

更新:UIButton 约束

两个按钮都有宽度和高度约束以及对视图底部的约束。左边的按钮对视图有一个前导约束。右边的一个对视图有一个尾随约束。如上所述,它们都对表格视图有水平间距约束。

【问题讨论】:

  • 我在没有 2 个按钮的情况下测试了您的布局,并将 tableview 的侧面固定到 superview 并且效果很好。这 2 个按钮发生了什么事。你能展示你所有的约束吗?
  • 嗯,这很奇怪。我认为这些限制没有任何问题。我添加了一个屏幕截图。希望对您有所帮助。
  • 按钮上方的两条垂直线是什么?
  • 它们是按钮和视图之间的前导和尾随约束
  • 我什至删除了后面的地图视图和按钮,看看它们是否与问题有关,但我遇到了同样的问题。

标签: ios objective-c xcode interface-builder nslayoutconstraint


【解决方案1】:

我认为你的问题出在这个方法调用上:

[UIView transitionWithView:self.categoryTableView duration:0.3f options:UIViewAnimationOptionCurveEaseOut animations:^{

        self.categoriesHeightConstraint.constant = 44.0f;
        [self.categoryTableView layoutIfNeeded];

    } completion:nil];

这应该可以满足您的需求:

self.categoriesHeightConstraint.constant = 44.0f;
[UIView animateWithDuration:0.3f animations:^{
    [self.view layoutIfNeeded];
    //This is assuming the method is called from a view controller.
    //You need to call layoutIfNeeded on the superview of what you're animating
}];

此外,Apple 文档说,在更改约束之前进行额外的 [self.view layoutIfNeeded] 调用是一种很好的形式,这样任何不完整的约束都会更改并得到更新。不过我会留给你。

【讨论】:

  • 它给出完全相同的结果。
【解决方案2】:

所以我想我得到了你正在寻找的功能:

我给表格视图设置了一个与按钮顶部相匹配的高度约束,但它可以是任何东西。

然后我得到表格应该是的计算高度并设置它:

tableArray = [[NSMutableArray alloc]init];
[tableArray addObject:@"Row 1"];
[tableArray addObject:@"Row 2"];
etc.......


double newHeight = ([tableArray count] * 44);
CGRect newFrame = CGRectMake(basicTable.frame.origin.x, basicTable.frame.origin.y, basicTable.frame.size.width, newHeight);
[basicTable setFrame:newFrame];

然后对于您的动画,我使用了 [UIView animateWithDuration],如图所示:

[UIView animateWithDuration:1.0 delay:0.0 options:UIViewAnimationOptionCurveEaseOut animations:^{

    tableHgtConst.constant = newHeight;
    [self.view layoutIfNeeded];

} completion:^(BOOL finished) {


}];

我只保留了底部约束,然后更改高度约束以匹配高度强制表格向上而不是向下。

屏幕: 模拟器:http://i.stack.imgur.com/T7MBr.png

代码文件:http://i.stack.imgur.com/GGOkq.png

【讨论】:

猜你喜欢
  • 2016-12-15
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多