![UIButton UIButton背景图片拉伸](http://iphone.xiaoxiaostudio.net/wp-content/uploads/2013/04/UIButton.png)
如果要实现如下图所示的button,背景是一个图片,由于按钮的大小不定,这时就需要适时的将背景的图片进行拉伸。
UIButton * btnIcon=[UIButton buttonWithType:UIButtonTypeCustom]; [btnIcon setFrame:CGRectMake(5, 50, 80, 30)]; // [btnIcon setImageEdgeInsets:UIEdgeInsetsMake(0, -10, 0, 0)]; 用于设置按钮左侧图标的位置 本例不需要 UIImage *image = [UIImage imageNamed:backImageFile]; //设置图片的拉伸方式,从图片的正中心拉伸,如果你的扩展位置不同,可以调整capInsets的值 //UIEdgeInsetsMake(CGFloat top, CGFloat left, CGFloat bottom, CGFloat right) 分别对应上,左,下,右的距离 image=[image resizableImageWithCapInsets:UIEdgeInsetsMake(floorf(image.size.height/2), floorf(image.size.width/2), floorf(image.size.height/2), floorf(image.size.width/2))]; //指定一下背景图片,就可以了。 [btnIcon setBackgroundImage:image forState:UIControlStateNormal];
![UIButton UIButton背景图片拉伸](http://iphone.xiaoxiaostudio.net/wp-content/uploads/2013/04/UIButton.png)
转载于:https://www.cnblogs.com/liuxingzi/archive/2013/04/09/3404335.html