我在调整按钮宽度大小的动画时遇到了一些麻烦。这是我的代码:
- (IBAction)profileButtonPressed:(UIButton *)sender {
UIImage *buttonProfileDefault = [[UIImage imageNamed:@"Profile_Button_Default"] resizableImageWithCapInsets:UIEdgeInsetsMake(3, 3, 3, 3)];
[sender setBackgroundImage:buttonProfileDefault forState:UIControlStateNormal];
UIImage *buttonProfileDefaultDown = [[UIImage imageNamed:@"Profile_Button_Default_Down"] resizableImageWithCapInsets:UIEdgeInsetsMake(3, 3, 3, 3)];
[sender setBackgroundImage:buttonProfileDefaultDown forState:UIControlStateHighlighted];
[sender setTitle:@"LOG OUT" forState:UIControlStateNormal];
sender.titleLabel.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight;
sender.imageView.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight;
// Animate
CGRect originalFrame = sender.frame;
originalFrame.size.width = sender.frame.size.width+60;
[UIView animateWithDuration:1.5 animations:^{
sender.frame = originalFrame;
}];
}
它改变了按钮的宽度(除非我去掉了改变标题的那段代码),但它并没有通过动画来做到这一点。
有什么建议可能是错的吗?
更新
所以我设法为宽度变化设置动画,但将 setTitle 放在动画部分,但它没有为文本本身获得正确的宽度(下图)。我怎样才能做到这一点?
解决方案
所以我最终以编程方式放置了uibutton。似乎自动布局导致了问题。
是我自己还是Objective C中有很多东西有点笨拙?!