Ios 调用[table reloadData]将删除表';s位置从[uiview animatewithduration]更改

Ios 调用[table reloadData]将删除表';s位置从[uiview animatewithduration]更改,ios,objective-c,uitableview,Ios,Objective C,Uitableview,有一个按钮可以水平移动我的桌子 - (void)togglePreferencePageVisibility:(id)sender { int translation = [self preferencesVisible] ? -PREFERENCE_TRANSLATION_HEIGHT : PREFERENCE_TRANSLATION_HEIGHT; //animate tableview down [UIView animateWithDuration:0.3

有一个按钮可以水平移动我的桌子

- (void)togglePreferencePageVisibility:(id)sender {
     int translation = [self preferencesVisible] ? -PREFERENCE_TRANSLATION_HEIGHT : PREFERENCE_TRANSLATION_HEIGHT;
    //animate tableview down
    [UIView animateWithDuration:0.3
                          delay:0
                        options: UIViewAnimationCurveEaseOut
                     animations:^{
                         _table.center = CGPointMake(_table.center.x, _table.center.y+translation);
                     }
                     completion:^(BOOL finished){

                     }];
}
当我调用
[\u table reloadData]
时,表将恢复到其原始位置。我试着手动调整桌子的位置,就像这样

NSLog(@"before %@", NSStringFromCGRect(_table.frame));
    [_table reloadData];
    _table.frame = CGRectMake(initialTablePosition.x, initialTablePosition.y+PREFERENCE_TRANSLATION_HEIGHT, _table.frame.size.width, _table.frame.size.height);
    NSLog(@"after %@", NSStringFromCGRect(_table.frame));

但是没有运气。此外,帧的前后y位置相同。

您还需要使用NSLayoutConstraint类调整自动布局约束。您可以添加、删除或更改NSLayoutConstraints的值。由于从iOS 7到iOS 8的变化,这是目前关于堆栈溢出的一个非常常见的问题

如果不调整布局约束,则视图将重新定位回其原始位置<代码>重新加载数据重新绘制UITableView,从而使视图层次结构重新检查其位置e.t.c

作为使宽度约束更窄的视图的示例:

CGRect frame = myView.frame;
frame.size.width -= 100;
//Next line now needed in iOS 8
myWidthConstraint.constant -= 100;
[UIView animateWithDuration:0.5 animations:^{
    [myView setFrame:frame];
}];
编辑:向后移动示例:

CGRect frame = myView.frame;
    frame.size.width += 100;
    //Next line now needed in iOS 8
    myWidthConstraint.constant += 100;
    [UIView animateWithDuration:0.5 animations:^{
        [myView setFrame:frame];
    }];

使用自动布局时,不应设置任何框架。如果执行此操作,当视图需要重新绘制自身时,框架将重置为由其约束定义的框架。您应该在表视图与其superview(在下面的示例中我将其称为topCon)的顶部(或底部)之间的约束中添加一个IBOutlet


我忘了提到的另一件事是,移动表格位置的按钮是一个“切换”按钮,因此它也可以将表格移回其原始位置。我只是不想让表自动从重载数据移回。自动布局仍然有效吗?每次重新定位视图时都需要调整自动布局。所以我假设如果你的桌子在一个地方,你把它移到另一个地方,反之亦然。i、 e.
if(isMoved){//moveback};else{//Move}如果是这种情况,那么是的,它将工作,但您需要重新执行约束。请参见我的编辑示例。此问题与iOS 8无关。使用布局约束时,不应直接修改框架。自从引入约束以来,情况就是这样。这个解决方案不是正确的方法;你根本不应该设置框架,只是修改约束。我遇到过一些人在修改约束时遇到问题,这种方法似乎很有效。
- (void)togglePreferencePageVisibility:(id)sender {
     int translation = [self preferencesVisible] ? -PREFERENCE_TRANSLATION_HEIGHT : PREFERENCE_TRANSLATION_HEIGHT;
    //animate tableview down
    self.topCon.constant += translation; // this might need to be "-=" depending on whether your constraint is to the top or bottom
    [UIView animateWithDuration:0.3
                          delay:0
                        options: UIViewAnimationCurveEaseOut
                     animations:^{
                         [self.view layoutIfNeeded];
                     }
                     completion:^(BOOL finished){

                     }];
}