在UIButton中加入一个UIImageView,拉伸imageView的UIimage,然后将button的背景设为clearColor等等。把imageView放入button中,
//刚才image拉伸的代码
//图片处理
CGFloat top = floorf(image.size.height/2); // 顶端盖高度
CGFloat bottom = image.size.height-(image.topCapHeight+1) ; // 底端盖高度
CGFloat left = floorf(image.size.width/2); // 左端盖宽度
CGFloat right = image.size.width - (image.leftCapWidth + 1); // 右端盖宽度
UIEdgeInsets insets = UIEdgeInsetsMake(top, left, bottom, right);
// 指定为拉伸模式,伸缩后重新赋值
image = [image resizableImageWithCapInsets:insets resizingMode:UIImageResizingModeStretch];
这是本人试出来的方法,也是唯一适合Button的方法,如果有更好的,欢迎大家提供下
本文介绍了一种在UIButton中加入UIImageView并实现图片拉伸的方法,通过指定CapInsets和设置resizingMode属性,使得UIImageView能够完美适配按钮大小。此外,还详细解释了如何将按钮背景设置为clearColor,提供了iOS开发中一个实用的UI布局技巧。
817

被折叠的 条评论
为什么被折叠?



