IOS按钮边框厚度问题,为什么?

时间:2014-08-11 06:31:57

标签: ios objective-c ios-ui-automation

UIButton 顶部边框看起来比以下更厚,但有时正确 ,为什么?

代码:

 UIImage * sanImage = [UIimage imageNamed:@"product_bt1_normal"];
 [self.saveBtn setBackgroundImage:[sanImage 
 stretchableImageWithLeftCapWidth:sanImage.size.width/3   
 topCapHeight:sanImage.size.height/3] forState:UIControlStateNormal];

enter image description here

2 个答案:

答案 0 :(得分:2)

你想制作一个按钮吗?如果是这样,也许使用UIButton代替?您可以使用button.layer.borderWidth = 1.0f

控制边框

如果您已开始使用图像,请创建UIImageView,并以此方式修改边框粗细:

UIImageView *iv = [[UIImageView alloc] initWithImage:sanImage];
[iv.layer setBorderWidth:0.5f];

答案 1 :(得分:0)

可能是因为像素外边界。由于您使用的是高度/ 3.0f,因此您的图像可能无法返回表现良好的图像。

此外,您应该使用一种新的可伸缩图像方法resizableImageWithCapInsets:。

请尝试使用此代码:

[self.saveBtn setBackgroundImage:[sanImage resizableImageWithCapInsets:UIEdgeInsetsMake(3.0f, 3.0f, 3.0f, 3.0f)] forState:UIControlStateNormal];

您可能需要稍微弄乱插图的值,我不知道按钮图像的尺寸。