[英]backgroundImage on UIButton scales wrongly
当用户按下按钮然后设置宽度动画时,我试图给我的UIButton一个新的背景图像。 问题是按钮没有缩放图像(我只使用视网膜图像)。
点击之前:
点击后:
以及使事情发生的代码:
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];
我希望这能帮到你......
创建边缘插入的方式将使最左边的3个和最右边的3个(或像素)保持不变,并且它会拉伸按钮上的头部。 如果你想让头部的大小保持不变,你必须将它包含在左侧,留下1个像素可拉伸,然后是右侧。 假设你的图片宽度为50,你会写:
UIImage * buttonProfileDefault = [[UIImage imageNamed:@"Profile_Button_Default"] resizableImageWithCapInsets:UIEdgeInsetsMake(3, 46, 3, 3)];
但是,这会使您的图片位于按钮的左侧。 我推荐的解决方案是使用2张图片:
代码看起来像:
UIImage *backgroundImage = [[UIImage imageNamed:@"border.png"] resizableImageWithCapInsets:UIEdgeInsetsMake(3, 3, 3, 3)]; // in this case you can use 3, 3, 3, 3
[self.profileButton setBackgroundImage:backgroundImage forState:UIControlStateNormal];
UIImage *titleImage = [UIImage imageNamed:@"head.png"];
[self.profileButton setImage:titleImage forState:UIControlStateNormal];
希望这可以帮助!
我已经修改了以下几点的代码:
现在检查:
UIImage *buttonProfileDefault = [[UIImage imageNamed:@"Profile_Button_Default.png"] resizableImageWithCapInsets:UIEdgeInsetsMake(3, 3, 3, 3)];
[self.profileButton setBackgroundImage:buttonProfileDefault forState:UIControlStateNormal];
[self.profileButton setBackgroundImage:buttonProfileDefault
forState:UIControlStateSelected];
[self.profileButton setBackgroundImage:buttonProfileDefault forState:UIControlStateHighlighted];
[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) {
}];
我认为问题是你改变背景然后调整大小。
让它使用自动布局的关键是为widthConstraint常量值设置动画,并在动画中设置背景图像。
例如,我的视图名称按钮上有一个按钮 ,我有一个名为buttonWidth的宽度约束的IBOutlet - 下面是我的buttonPress的代码:
UIImage *imageButton = [[UIImage imageNamed:@"button"] resizableImageWithCapInsets:UIEdgeInsetsMake(0, 10, 0, 10)];
[UIView animateWithDuration:0.01f
animations:^{
[self.button setBackgroundImage:imageButton forState:UIControlStateNormal];
}
completion:^(BOOL finished) {
[UIView animateWithDuration:1.0f
animations:^{
self.buttonWidth.constant = 300;
[self.view layoutIfNeeded];
}
completion:^(BOOL finished) {
}];
}];
正如您在上面的代码中看到的那样,使其适用于我的关键是在动画中设置按钮背景,出于某种原因,如果我在动画之外更改背景图像,则在动画完成之前它将无法正确设置。
使用autoLayout时设置框架对我来说也不起作用,所以我为约束常量设置了动画
试试这个,而不是将图像指定为
UIImage *buttonProfileDefault = [[UIImage imageNamed:@"Profile_Button_Default"] resizableImageWithCapInsets:UIEdgeInsetsMake(3, 3, 3, 3)];
做这个
UIImage *buttonProfileDefault = [UIImage imageNamed:@"Profile_Button_Default"];
无需设置resizableImageWithCapInsets的约束。
我有同样的问题,但它发生在我的应用程序加载时,我在检查器中弄乱了button
的属性,如果你向下滚动并autoresize subviews
检查autoresize subviews
它可能会工作,它对我autoresize subviews
! 祝好运!
在使用笔尖时,我有类似的情况调整子视图的大小。 不确定你是不是,但解决我的问题的是在我的各种观点中取消选中“使用Autolayout”。
希望能解决您的问题。
布兰登
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.