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

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

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

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

怪我咯
怪我咯

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

reply all(1)
Peter_Zhu

(1) background image, title, image (foreground image) have z-index, which has been set when the UIButton is created, so the effects of setBackgroundImage and setImage are different.
(2) Regarding the Image and title in UIButton, Apple’s official documentation explains it in detail:
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" The Content section above explains that title and image are UILabel and UIImageView respectively
(2.2) "Configuring Edge Insets" There are three properties in it contentEdgeInsets, titleEdgeInsets, imageEdgeInsets, adjust the values ​​​​to achieve left text and right image.
(2.3) UIImageView also provides detailed instructions on how to resize using resizableImageWithCapInsets:resizingMode::
https://developer.apple.com/library/ios/documentation/UIKit/Reference/UIImageView_Class/index.html#// apple_ref/occ/cl/UIImageView
(2.4) The content display mode of the image is determined by the contentMode of UIImageView;
Obviously the background image uses the default mode UIViewContentModeScaleToFill.
Please refer to the example:
http://blog.csdn.net/iunion/article/details/7494511

Please refer to the following code and modify the contentMode before setImage to see what the difference will be:

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

(3) It is best to process the picture to a suitable small size in advance, without the confusion you are encountering now. If you use large images now, you have to write a bunch of code for image cropping. Here is a link to UIImage category for your reference:
http://blog.sina.com.cn/s/blog_6123f9650100p88p.html

Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!