Dev*_*Dev 2 cocoa-touch uibutton ios
如何创建一个UIButton由以下组成的背景图像:
比如下面的例子?

编辑:我没有意识到在中心没有N个重复的图像,但只有一个拉伸的图像.看到接受的答案.
据我所知,它无法完成.您可以做的是拉伸图像,但不能添加n个中间图像.
在其间添加可伸缩图像的代码是
//Create an image - Where UIEdgeInsets is in top left bottom right
UIImage* buttonImage = [[UIImage imageNamed:@"button.png"] resizableImageWithCapInsets:UIEdgeInsetsMake(0, 16, 0, 16)];
// Create a custom buttom
UIButton *myButton = [UIButton buttonWithType:UIButtonTypeCustom];
myButton.frame = CGRectMake(0, 0, 100, buttonImage.size.height);
[myButton setBackgroundImage:buttonImage forState:UIControlStateNormal];
[myButton setTitle:@"Button" forState:UIControlStateNormal];
//Add it to view - if it is a view controller self.view
[self addView:myButton];
Run Code Online (Sandbox Code Playgroud)
来自Apple的UIImage类参考:
" resizableImageWithCapInsets:
您可以使用此方法为图像添加上限,或更改图像的现有上限.[...]在缩放图像或调整图像大小时,标题覆盖的区域不会缩放或调整大小.相反,每个方向未被盖子覆盖的像素区域是平铺的,从左到右和从上到下,以调整图像大小." [强调添加]
您可以在http://mobiledevelopertips.com/user-interface/ios-5-uiimage-and-resizableimagewithcapinsets.html上阅读更多相关信息.
| 归档时间: |
|
| 查看次数: |
965 次 |
| 最近记录: |