iOS gt;gt; UIButton ImageView Property gt;gt; How to set Content Mode?(iOSgt;gt;UIButton ImageView 属性gt;gt;如何设置内容模式?)
问题描述
有没有办法设置 UIButton Image、BackgroundImage 或 ImageView 属性 Content Mode 属性?
Is there a way to set a UIButton Image, BackgroundImage or ImageView Properties Content Mode Property?
我试过直接的方法(当然没用……):
I've tried the direct approach (it didn't work, of course...):
self.imageButton.imageView.contentMode = UIViewContentModeScaleAspectFit;
有一个带有图片的按钮很好,但是如果没有办法正确设置它,它就不是很方便......
It's nice to have a button with an image, but if there's no way to set it properly, it's not very handy...
推荐答案
使用iOS7/8 自动布局,button.imageView
button
无法缩放布局,例如iPhone 6:
Using iOS7/8 with auto-layout, button.imageView
doesn't get scaled when button
is laid out, e.g. for iPhone 6:
(lldb) po button
<UIButton: 0x7fb4f501d7d0; frame = (0 0; 375 275); opaque = NO; autoresize = RM+BM; tag = 102; layer = <CALayer: 0x7fb4f501d160>>
(lldb) po button.imageView
<UIImageView: 0x7fb4f51d21f0; frame = (0 0; 0 0); clipsToBounds = YES; hidden = YES; opaque = NO; userInteractionEnabled = NO; layer = <CALayer: 0x7fb4f5152860>>
设置button
的图片后,button.imageView
假设图片的大小,e.g.对于 320x240 图像:
After setting button
's image, button.imageView
assumed the size of the image, e.g. for a 320x240 image:
(lldb) po button.imageView
<UIImageView: 0x7fb4f51d21f0; frame = (27.5 17.5; 320 240); clipsToBounds = YES; opaque = NO; userInteractionEnabled = NO; layer = <CALayer: 0x7fb4f5152860>>
看起来button.imageView
不尊重它的内容模式,其实问题出在button.imageView
的大小.
It looks like button.imageView
does not respect its content mode, but actually, it is the size of the button.imageView
that is the problem.
答案也是设置按钮
的内容对齐方式.
The answer is to set button
's content alignment as well.
下面设置button
的图片,设置button.imageView
的内容模式,使button.imageView
适合大小按钮
:
The following sets button
's image, sets button.imageView
's content mode, and makes button.imageView
fit the size of button
:
[button setImage:image forState:UIControlStateNormal];
button.imageView.contentMode = UIViewContentModeScaleAspectFill;
button.contentHorizontalAlignment = UIControlContentHorizontalAlignmentFill;
button.contentVerticalAlignment = UIControlContentVerticalAlignmentFill;
现在,button.imageView
与 button
大小相同,并且 具有所需的内容模式.
Now, button.imageView
is the same size as button
, and has the desired content mode.
(lldb) po button.imageView
<UIImageView: 0x7faac219a5c0; frame = (0 0; 375 275); clipsToBounds = YES; opaque = NO; userInteractionEnabled = NO; layer = <CALayer: 0x7faac219a6c0>>
从而达到预期的结果.很方便!
The desired result is thereby achieved. Very handy!
这篇关于iOS>>UIButton ImageView 属性>>如何设置内容模式?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:iOS>>UIButton ImageView 属性>>如何设置内容模式?
基础教程推荐
- 如何在没有IB的情况下将2个按钮添加到右侧的UINavigationbar? 2022-01-01
- Android:对话框关闭而不调用关闭 2022-01-01
- 如何让对象对 Cocos2D 中的触摸做出反应? 2022-01-01
- Kivy Buildozer 无法构建 apk,命令失败:./distribute.sh -m “kivy"d 2022-01-01
- 当从同一个组件调用时,两个 IBAction 触发的顺序是什么? 2022-01-01
- 在 gmail 中为 ios 应用程序检索朋友的朋友 2022-01-01
- 如何在 iPhone 上显示来自 API 的 HTML 文本? 2022-01-01
- android 应用程序已发布,但在 google play 中找不到 2022-01-01
- UIWebView 委托方法 shouldStartLoadWithRequest:在 WKWebView 中等效? 2022-01-01
- 如何在 UIImageView 中异步加载图像? 2022-01-01