如何在iPhone上指定具有不同左右帽尺寸的可伸缩图像

时间:2011-09-15 19:00:51

标签: iphone uibutton uiimage

我正在为iPhone实施下拉菜单。图像如下所示:

[5px角落] [1 px可伸展区域] [带角落的10px箭头]

我想使用类似的东西:

UIButton *dd = [[UIButton alloc] initWithFrame:CGRectMake(0, 0, 200, 20)];
dd.autoresizingMask=UIViewAutoresizingFlexibleWidth;
[dd setBackgroundImage:[[UIImage imageNamed:@"dd"] stretchableImageWithLeftCapWidth:5      topCapHeight:0] forState:UIControlStateNormal];

我似乎无法找到指定右边距的方法。文档说它是从左边距计算的。必须有一种方法可以获得具有不同左右固定尺寸的可伸缩图像?绘制9个补丁图像可能吗?

任何人都可以在iphone上找到解决方案吗?

2 个答案:

答案 0 :(得分:4)

左帽后面的下一个像素是唯一水平拉伸的像素。因此,右侧的10个像素以其原始大小保留,并且没有任何意义的右侧帽。

如果您的图像有任何失真是由于其他原因。这应该有效:

UIButton *dd = [UIButton buttonWithType:UIButtonTypeCustom];
[dd setFrame:CGRectMake(0, 0, 200, 20)];
[dd setBackgroundImage:[[UIImage imageNamed:@"dd.png"] stretchableImageWithLeftCapWidth:5.0 topCapHeight:0.0] forState:UIControlStateNormal];
[self.view addSubview:dd];

答案 1 :(得分:0)

您仍然会看到失真的原因是因为原始图像比您指定的宽度宽(如果失真是垂直的,则为高度)。例如。如果你想要一个宽度为200px但原始图像宽度为300px的按钮,即使将它用作可伸缩图像,也会看到失真。从一个短的图像开始,可以使用指定的封面插入拉伸到您想要的任何尺寸,例如宽度为10px的图像可以满足您的需求。