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

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

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) “Conguring Edge 有三個屬性” contentEdgeInsets, titleEdgeInsets, imageEdgeInsets, 調整值可以實現左文字右圖片。
(2.3) UIImageView 關於用resizableImageWithCapInsets:resizingMode: 如何調整大小,也作了詳細說明:
https://developer.apple.com/library/ios/documentation/UIKit/Reference/UIImageView_Class/index.htmlios/ 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學習者快速成長!