ios - 关于button得image和backgroundimage的区别
怪我咯
怪我咯 2017-04-17 17:44:31
[iOS讨论组]

我在网上搜了一下,基本都是说

setBackgroundImage,image会随着button的大小而改变,图片自动会拉伸来适应button的大小,这个时候任然可以设置button的title,image不会挡住title;

相反的的setImage,图片不会进行拉伸,原比例的显示在button上,此时再设置title,title将无法显示,
我想问的是这些区别的原始出处是哪里? 我在uibutton的 官方的 class reference里都没找到

怪我咯
怪我咯

走同样的路,发现不同的人生

全部回复(1)
天蓬老师

(1) background image, title, image(前景图片) 是有z-index的, 在UIButton建立的时候就已经定好了,所以setBackgroundImage 和 setImage的效果是不一样的。
(2) 关于UIButton里的Image和title, apple 官方文档解释的还是很详细的:
https://developer.apple.com/library/ios/documentation/UIKit/Reference/UIButton_Class/#//apple_ref/doc/uid/TP40006815-CH3-SW31
(2.1) "Figure 1Providing a title and image for a button" 上面 Content部分解释了 title和image分别是 UILabel和 UIImageView
(2.2) “Configuring Edge Insets” 里面有三个属性 contentEdgeInsets, titleEdgeInsets, imageEdgeInsets, 调整值可以实现左文字右图片。
(2.3) UIImageView 关于用 resizableImageWithCapInsets:resizingMode: 如何调整大小,也作了详细说明:
https://developer.apple.com/library/ios/documentation/UIKit/Reference/UIImageView_Class/index.html#//apple_ref/occ/cl/UIImageView
(2.4) image的内容展现模式由UIImageView的contentMode决定;
显然background image使用了默认模式UIViewContentModeScaleToFill。
请参考看实例:
http://blog.csdn.net/iunion/article/details/7494511

请参考以下代码,在setImage之前先修改contentMode,看看会有什么不同:

//模式1.
self.btnTestImage.imageView.contentMode = UIViewContentModeScaleAspectFit;
//模式2.
//self.btnTestImage.imageView.contentMode=UIViewContentModeScaleAspectFill;
[self.btnTestImage setImage:imgW200 forState:UIControlStateNormal];

(3) 图片最好还是事先处理到合适的小尺寸,没有你现在遇到的困惑。你现在用大图片,就要写一堆图片裁减的代码。给你个UIImage category的链接作参考:
http://blog.sina.com.cn/s/blog_6123f9650100p88p.html

热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板
关于我们 免责申明 意见反馈 讲师合作 广告合作 最新更新
php中文网:公益在线php培训,帮助PHP学习者快速成长!
关注服务号 技术交流群
PHP中文网订阅号
每天精选资源文章推送
PHP中文网APP
随时随地碎片化学习
PHP中文网抖音号
发现有趣的

Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号