iphone - UIButton 上的 backgroundImage 缩放错误

标签 iphone objective-c ios xcode storyboard

我试图在用户按下按钮时为我的 UIButton 提供新的背景图像,然后设置宽度动画。问题是按钮不缩放图像(我只使用视网膜图像)。

点击前:

enter image description here

点击后:

enter image description here

以及让事情发生的代码:

UIImage *buttonProfileDefault = [[UIImage imageNamed:@"Profile_Button_Default"] resizableImageWithCapInsets:UIEdgeInsetsMake(3, 3, 3, 3)];
        [self.profileButton setBackgroundImage:buttonProfileDefault forState:UIControlStateNormal];

[self.profileButton removeConstraint:self.profileButtonConstraint];

// Animate
CGRect originalFrame = self.profileButton.frame;
originalFrame.size.width = 40;
[UIView animateWithDuration:0.2f
    animations:^{
        self.profileButton.frame = originalFrame;
    }
        completion:^(BOOL finished) {
    }];

最佳答案

试试这个代码...

    [UIView beginAnimations:nil context:NULL];
    [UIView setAnimationDuration:1.0];
    [self.profileButton setBackgroundImage:[UIImage imageNamed:@"Profile_Button_Default"] forState:UIControlStateNormal];
    [self.profileButton setBackgroundImage:[UIImage imageNamed:@"Profile_Button_Default"] forState:UIControlStateSelected];
    [self.profileButton setFrame:CGRectMake(self.profileButton.frame.origin.x, self.profileButton.frame.origin.y, self.profileButton.frame.size.width + 40, self.profileButton.frame.size.height)];
    [UIView commitAnimations];

希望对你有帮助...

关于iphone - UIButton 上的 backgroundImage 缩放错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13617286/

相关文章:

iphone - 将记录保存到 coreData 需要很长时间,我怎样才能更快地插入它们?

iphone - 使用 NSXMLParser 解析时,它是下载整个 .xml 然后解析,还是进行流式解析?

ios - 如何使用 HKObserverquery 读取 iOS Healthkit 血压(收缩压,舒张压)?

iphone - 如何从音频队列缓冲区中提取整数样本并将修改后的样本写回?

ios - Swift Firebase 为 UIActivityViewController 附加图像

objective-c - 使用最简单的方法将数据(字符串)从子 VC 传递到父 VC

iphone - 以编程方式将 UITextField 设置为编辑模式

iphone - 安装时将数据传递到我的应用程序

ios - UICollectionView 一秒钟以错误的顺序加载图像

objective-c - iOS 在 View 之间传递消息,基于窗口的应用程序