更改自动布局启用控件的位置还原更改?

时间:2014-08-20 12:32:55

标签: ios uicollectionview autolayout

我的应用程序的一部分是在UIcollectionView中显示一个列表,如果用户向下滑动屏幕,我将在viewController的顶部显示带有动画的UISearchBar。 我在一个单独的viewController中完成了所有这些操作。

我使用autolayout来定位viewController中的控件。

我将搜索栏从y = -88移动到y = 0并向下移动collectionView以在用户向下滑动屏幕时为searchBar腾出空间,我的问题是如果我调用reloadData到collectionView,搜索栏和CollectionView都移动到之前的自动覆盖位置

我使用下面的动画来移动它们

[UIView animateWithDuration:0.4 animations:^(void){
        fileSearchBar.frame=fileSearchBar.frame=CGRectOffset(fileSearchBar.frame, 0,88);
        chartsCollectionView.frame=gridFrame;///gridFrame is calculated to move down
    }]; 

我有办法阻止他们移动到预先计算的位置

1 个答案:

答案 0 :(得分:0)

使用自动布局时,不得设置框架。您必须更改约束并让间接更改帧。

Apple的自动布局指南显示动画的an example

[containerView layoutIfNeeded]; // Ensures that all pending layout operations have been completed
[UIView animateWithDuration:0.4 animations:^{
     // Make all constraint changes here
     [containerView layoutIfNeeded]; // Forces the layout of the subtree animation block and then captures all of the frame changes
}];

您没有显示如何设置约束,因此我无法向您展示如何修改它们以移动搜索栏和集合视图。理想情况下,您只需要设置其中一个约束的constant属性。 (你的约束应该被设置,以便移动一个东西导致另一个自动调整自己。)

相关问题