2012-11-29 51 views
6

我试图给我的UIButton一个新的背景图像,当用户按下按钮,然后动画的宽度。问题是该按钮不能缩放图像(我只使用视网膜图像)。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) { 
    }]; 
+0

我希望我们能想出解决办法:我有同样的问题。我已经尝试了所有contentMode选项和所有imageEdgeInsets(对于那些将我的背景图像移动到按钮图像中,覆盖或消除按钮标题)的所有都无济于事。 – tobinjim

+0

我会为此付出代价。真的需要知道它。 –

+0

@HolgerEdwardWardlowSindbæk尝试我的代码... –

回答

1

我也有同样的问题,但它发生在我的应用程序加载时,我在检查器中与button的属性混为一谈,如果向下滚动并取消选中autoresize subviews它可能工作,它为我做了!祝你好运!

+0

我非常希望这会为我解决同样的问题,但事实并非如此。我的背景图像是由UIImageView的子类派生的,但是,像这样:[self setBackgroundImage:[backgroundImageView image] forState:UIControlStateNormal]; – tobinjim

+0

@tobinjim是否尝试将其Style属性从Plain更改为Custom? – Shinigamae

+0

@Shinigamae我认为我总是习惯。我最终抛弃了使用UIButton的子类的概念,并去了处理触摸的UIView子类。我最初喜欢UIButton类,因为标题文本跟踪控制状态;但随后当我测试我的程序时,它变得令人讨厌,标题更改非常突然,所以在我的UIView子类中,我处理相同的消息,但是快速淡出并转换为更改文本。 – tobinjim

2

您创建边缘插图的方式会保持最左边的3和最右边的3个点(或像素)不变,并且它会拉伸按钮上的头部。如果您想让头部的大小保持不变,则必须将其包含在左侧,使1像素可伸展,然后出现在右侧。假设你的图片的宽度是50,你可以这样写:

UIImage * buttonProfileDefault = [[UIImage imageNamed:@"Profile_Button_Default"] resizableImageWithCapInsets:UIEdgeInsetsMake(3, 46, 3, 3)];

但是,这将使你的图片放置在按钮的左侧。我建议的解决办法是使用2个图像:

  1. 仅含按钮的边界,设置为按钮的背景图像的可调整大小的图像
  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];

希望这有助于!

+0

这是一个非常好的观察。这不是问题。在开始时,我将背景图像切换为没有头像的问题(问题在那里解决),但是它没有使用cap-inset,所以边缘变得扭曲和怪异。那就是问题所在!感谢您的输入。 –

+0

什么是您的图像的原始大小? – Levi

+0

70 x 58(我只使用视网膜版本的视网膜和非视网膜iPhone) –

1

我在使用笔尖时调整了子视图的大小。不知道你是不是,但解决我的问题是什么在我的各种意见没有选中“使用Autolayout”。

希望能够解决您的问题。

布兰登

+0

这可能会解决这个问题,但我想使用自动布局。如果你没有选中它,那就是整个应用程序! –

+0

AutoLayout选择仅适用于您检查或取消选中的笔尖。您可以使用自动布局设置几个笔尖,有些不在同一个应用中使用自动布局 –

2

我修改一下几点代码:

  • 提供的图像Profile_Button_Default.png(或全名。JPG等),
  • 为图像添加状态为正常的情况下,这些影响不会出现在按钮的启动感动

立即检查:

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) { 
       }]; 
1

你能不能复印的图像,并调整到正确的按钮大小?

退房IWasRobbed“S在这个post.

答案,他有一个很好的功能,有你和声称使用这种方法对他的按钮。

+0

使用按钮图像可能比较合理,但是我发现这些方法比我想要的更大的图像使用更多的内存。它会显示活动监视器工具中的巨大峰值,并且我不断收到大量的内存警告。 – AlleyGator

2

我认为这个问题是不断变化的背景和然后调整。

的敲门砖,它在动画,支持自动布局的工作就是动画widthConstraint恒定值,也可以设置背景图片。

例如,我有和宽松N于我的视图名称按钮,我有一个IBOutlet到我的命名buttonWidth宽度约束 - 下面是我的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并没有为我工作,所以我的动画约束不断

2

试试这个,而不是分配图像

UIImage *buttonProfileDefault = [[UIImage imageNamed:@"Profile_Button_Default"] resizableImageWithCapInsets:UIEdgeInsetsMake(3, 3, 3, 3)]; 

做到这一点

UIImage *buttonProfileDefault = [UIImage imageNamed:@"Profile_Button_Default"]; 
还设置了框

不需要设置resizableImageWithCapInsets的约束。

3

试试这个代码...

[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]; 

我希望这会帮助你...

+0

如果您遇到任何问题,请尝试并留下评论。 –