为什么UIButton框架被setTitle:forState:方法重置

问题如下:

在某个时刻,button的位置被改变。 在此之后,button的标题也必须改变。

位置被animation(UIViewanimation块)改变,当标题被设置时,帧返回原始值(在animation之前)。

所以……我该如何解决这个问题? 为什么会发生?

更新代码

CGRect buttonFrame = self.button.frame; buttonFrame.origin = CGPointMake(16, 80); [UIView animateWithDuration:.4 animations:^{ [self.button setFrame:buttonFrame]; }completion:^(BOOL finished){ [self.button setTitle:@"Title" forState:UIControlStateNormal]; // can be here... }]; 

不要紧,我把setTitle:ForState:方法,框架回原来的价值。

尝试在你的UIViewController上禁用Autolayout。 对我而言,有时Autolayout会导致一些烦人的布局行为。 特别是当我使用animation。

编辑

如果Autolay为ON,这对我有用:

 [UIView animateWithDuration:.4 animations:^{ self.button.transform = CGAffineTransformMakeTranslation(-200,-200); }completion:^(BOOL finished) { [self.button setTitle:@"YourTitle" forState:UIControlStateNormal]; }]; 

当你使用自动布局时,你不应该设置任何框架。 相反,如果你想改变button的位置,你应该让IBOutlets在IB中约束并修改它们的常量。 例如,如果你的button对屏幕的左上angular有一个约束(我会称它们为leftCon和topCon),你可以这样做:

 [UIView animateWithDuration:.4 animations:^{ self.leftCon.constant = 16; self.topCon.constant = 80; [self.view layoutIfNeeded]; }]; 

尝试使用transform而不是改变框架:

button.transform = CGAffineTransformMakeTranslation(-100,-100);