UIButton with resizable图片

问题描述:

好吧,伙计,UIButton with resizable图片

我知道我做错了什么,但我无法弄清楚。这里是代码,将 把正常按钮状态的可调整大小的图像。

self.loginButton = [UIButton buttonWithType:UIButtonTypeCustom]; 
    self.loginButton.frame = CGRectMake(0.0, 44.0, 314, 44); 
    UIImage *image = [[UIImage imageNamed:@"loginbutton_image"] resizableImageWithCapInsets:UIEdgeInsetsMake(0.0,10.0,0.0,10.0)]; 
    [self.loginButton setImage:image forState:UIControlStateNormal]; 
    [self addSubview:self.loginButton]; 

这里是图像

enter image description here

任一图像的资产是错误或代码。

图像的宽度为21px。 右上角的10,& 10左侧1Px调整大小。

做任何一个地方我做错了什么。

感谢所有通过 阿伦

+0

你确定'image'不是'nil'? – 2012-07-11 01:04:18

+0

是的,我检查它。都很好。 – 2012-07-11 01:05:39

+0

http://i.imgur.com/jpj1l.png这是它的外观。 – 2012-07-11 01:05:56

阻止你应该这样来做:

self.loginButton = [UIButton buttonWithType:UIButtonTypeCustom]; 
    self.loginButton.frame = CGRectMake(0.0, 44.0, 314, 44); 
    UIImage *image = [[UIImage imageNamed:@"loginbutton_image"] resizableImageWithCapInsets:UIEdgeInsetsMake(0.0,10.0,0.0,10.0)]; 
    [self.loginButton setBackgroundImage:image forState:UIControlStateNormal]; 
[self.view addSubview:self.loginButton]; 

这样,你要设置按钮的背景图像属性。背景图像已经附加到按钮视图,所以不需要添加子视图。

+0

我真的很抱歉。我实际上是为按钮状态设置图像。 – 2012-07-11 01:15:10

+0

没问题,但是您发布的图片似乎是背景图片。如果您制作UIButtonTypeCustom,则该按钮不会有背景。如果您想制作一个样式化的按钮,将该图片作为背景,您应该使用我发布的代码。 – tomidelucca 2012-07-11 01:23:55

+0

代码与实际代码有何不同? – 2012-07-11 01:33:00

尝试

self.loginButton = [UIButton buttonWithType:UIButtonTypeCustom]; 
    self.loginButton.frame = CGRectMake(0.0, 44.0, 314, 44); 
    UIImage *image = [[UIImage imageNamed:@"loginbutton_image"] stretchableImageWithLeftCapWidth:10 topCapHeight:10]; //assuming the height is 44 
    [self.loginButton setImage:image forState:UIControlStateNormal]; 
    [self addSubview:self.loginButton]; 
+0

同样的结果。但是在iOS 5.0中不推荐使用stretchableImageWithLeftCapWidth方法。所以它不是一个好主意。 – 2012-07-11 01:31:21