问题如下:
在某个时刻,按钮的位置发生了变化。在此之后,按钮的标题也必须更改。
位置由动画(UIView 动画 block )改变,当设置标题时,帧返回到原始值(在动画之前)。
那么...我该如何解决?为什么会这样?
更新代码
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: 方法放在哪里,框架都会恢复到原始值。
最佳答案
当您使用自动布局时,您不应设置任何框架。相反,如果你想改变按钮的位置,你应该使 IBOutlets 成为 IB 中的约束并修改它们的常量。例如,如果您的按钮在屏幕的顶部和左侧有约束(我将它们称为 leftCon 和 topCon),您可以这样做:
[UIView animateWithDuration:.4 animations:^{
self.leftCon.constant = 16;
self.topCon.constant = 80;
[self.view layoutIfNeeded];
}];
关于ios - 为什么 UIButton 框架被 setTitle : forState: method 重置,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17997163/