ios - 关于button得image和backgroundimage的区别
怪我咯
怪我咯 2017-04-17 17:44:31
0
1
525

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

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

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

怪我咯
怪我咯

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

모든 응답(1)
Peter_Zhu

(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

최신 다운로드
더>
웹 효과
웹사이트 소스 코드
웹사이트 자료
프론트엔드 템플릿
회사 소개 부인 성명 Sitemap
PHP 중국어 웹사이트:공공복지 온라인 PHP 교육,PHP 학습자의 빠른 성장을 도와주세요!