简体   繁体   English

UIButton上的backgroundImage错误地缩放

[英]backgroundImage on UIButton scales wrongly

I'm trying to give my UIButton a new background image when the user pressed the button and then animate the width. 当用户按下按钮然后设置宽度动画时,我试图给我的UIButton一个新的背景图像。 Problem is that the button doesn't scale the image (I'm only using retina images). 问题是按钮没有缩放图像(我只使用视网膜图像)。

Before click: 点击之前:

在此输入图像描述

After click: 点击后:

在此输入图像描述

And the code that makes things happen: 以及使事情发生的代码:

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

try this code... 试试这个代码......

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

i hope this help you... 我希望这能帮到你......

The way you are creating the Edge Insets, would keep the leftmost 3 and the rightmost 3 poins (or pixels) constant, and it would stretch the head on the button. 创建边缘插入的方式将使最左边的3个和最右边的3个(或像素)保持不变,并且它会拉伸按钮上的头部。 If you want the head's size to be constant, you have to include it in the left side, leave 1 pixel stretchable and then comes the right side. 如果你想让头部的大小保持不变,你必须将它包含在左侧,留下1个像素可拉伸,然后是右侧。 Assuming your picture's width is 50, you would write: 假设你的图片宽度为50,你会写:

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

However, this would make your picture positioned in the left side of the button. 但是,这会使您的图片位于按钮的左侧。 The solution I recommend would be to use 2 images: 我推荐的解决方案是使用2张图片:

  1. a resizable image containing only the border of the button, set as the button's background image 仅包含按钮边框的可调整大小的图像,设置为按钮的背景图像
  2. an image containing only the head, set as the buttons image with constant size 仅包含头部的图像,设置为具有恒定大小的按钮图像

The code would look something like: 代码看起来像:

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

Hope this helps! 希望这可以帮助!

I have modified your code on following points: 我已经修改了以下几点的代码:

  • provided complete name of image Profile_Button_Default.png (or .jpg etc), 提供了图像Profile_Button_Default.png(或.jpg等)的完整名称,
  • added states for image as in case of normal it these effects will not be appeared in starting of button touched 为正常情况添加图像状态,这些效果将不会出现在触摸按钮的开始

Now Check: 现在检查:

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

I think the problem is that your changing the background and then resizing. 我认为问题是你改变背景然后调整大小。

The key to getting it working with auto layout is to animate the widthConstraint constant value and also set the background image in an animation. 让它使用自动布局的关键是为widthConstraint常量值设置动画,并在动画中设置背景图像。

For example, I have a button on my view name button , I have an IBOutlet to my width constraint named buttonWidth - Below is the code for my buttonPress : 例如,我的视图名称按钮上有一个按钮 ,我有一个名为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) {
                                      }];


                 }];

As you can see with my code above, the key to making it work for me was setting the button background inside an animation also, for some reason if i changed the background image outside the animation it would not set it correctly until the animation completed. 正如您在上面的代码中看到的那样,使其适用于我的关键是在动画中设置按钮背景,出于某种原因,如果我在动画之外更改背景图像,则在动画完成之前它将无法正确设置。

Also setting the frame when using autoLayout didn't work for me, so I animated the constraint constant 使用autoLayout时设置框架对我来说也不起作用,所以我为约束常量设置了动画

Try this, instead of assigning the image as 试试这个,而不是将图像指定为

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

do this 做这个

UIImage *buttonProfileDefault = [UIImage imageNamed:@"Profile_Button_Default"];

no need for setting constraint of resizableImageWithCapInsets. 无需设置resizableImageWithCapInsets的约束。

I had this same problem, but it happened when my app loaded up, I messed around with the button 's attributes in the inspector and if you scroll down and Un-check autoresize subviews it might work, it did for me! 我有同样的问题,但它发生在我的应用程序加载时,我在检查器中弄乱了button的属性,如果你向下滚动并autoresize subviews检查autoresize subviews它可能会工作,它对我autoresize subviews Good Luck! 祝好运!

I've had a similar situation resizing subviews when using a nib. 在使用笔尖时,我有类似的情况调整子视图的大小。 Not sure if you are or not, but what fixed my problems was unchecking 'Use Autolayout' on my various views. 不确定你是不是,但解决我的问题的是在我的各种观点中取消选中“使用Autolayout”。

Hopefully that fixes your issues. 希望能解决您的问题。

-Brandon 布兰登

Could you not make a copy of the image and resize it to the correct button size? 您是否可以复制图像并将其调整为正确的按钮大小?

Check out IWasRobbed "s answer in this post. 在这篇文章中查看IWasRobbed的答案

He has a nice function there for you and claims to use this method for his buttons. 他有一个很好的功能,并声称他的按钮使用这种方法。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM