Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/ios/115.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
iOS>&燃气轮机;UIButton ImageView属性>&燃气轮机;如何设置内容模式?_Ios_Uiimageview_Uibutton_Contentmode - Fatal编程技术网

iOS>&燃气轮机;UIButton ImageView属性>&燃气轮机;如何设置内容模式?

iOS>&燃气轮机;UIButton ImageView属性>&燃气轮机;如何设置内容模式?,ios,uiimageview,uibutton,contentmode,Ios,Uiimageview,Uibutton,Contentmode,有没有办法设置UIButton图像、BackgroundImage或ImageView属性内容模式属性 我尝试过直接方法(当然,它不起作用……): 有一个带有图像的按钮是很好的,但是如果没有办法正确设置它,它就不是很方便了…如果您不想对UIButton进行子类化,那么您可以试试这个 UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom]; for (UIView *view in button.subview

有没有办法设置UIButton图像、BackgroundImage或ImageView属性内容模式属性

我尝试过直接方法(当然,它不起作用……):


有一个带有图像的按钮是很好的,但是如果没有办法正确设置它,它就不是很方便了…

如果您不想对UIButton进行子类化,那么您可以试试这个

    UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
    for (UIView *view in button.subviews) {
        if ([view isKindOfClass:[UIImageView class]]) {
            [view setContentMode:UIViewContentModeScaleAspectFit];
        }
    }

使用带有自动布局的iOS7/8时,
按钮.图像视图
在布局
按钮
时不会缩放,例如对于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>>
它看起来像
按钮。imageView
不考虑其内容模式,但实际上,问题在于
按钮的大小。imageView

答案是设置
按钮的内容对齐方式。

以下设置
按钮的图像,设置
按钮.imageView
的内容模式,并使
按钮.imageView
适合
按钮的大小:

[button setImage:image forState:UIControlStateNormal];
button.imageView.contentMode = UIViewContentModeScaleAspectFill;
button.contentHorizontalAlignment = UIControlContentHorizontalAlignmentFill;
button.contentVerticalAlignment = UIControlContentVerticalAlignmentFill;
现在,
按钮.imageView
按钮的大小相同,具有所需的内容模式

(lldb) po button.imageView
<UIImageView: 0x7faac219a5c0; frame = (0 0; 375 275); clipsToBounds = YES; opaque = NO; userInteractionEnabled = NO; layer = <CALayer: 0x7faac219a6c0>>
(lldb)po按钮.imageView

从而达到了预期的效果。非常方便

每个UIButton都有一个自己的隐藏UIImageView。所以我们必须按照下面给出的方式设置内容模式

[[btn imageView] setContentMode: UIViewContentModeScaleAspectFit];

“系统按钮的属性值为零。”来自Apple Development library。我过去处理此类问题的另一种方法是创建UIImageView,在其中添加我的图片,然后在UIImageView的顶部添加一个自定义空白UIButton。当用户触摸图像时,他实际上是在触摸按钮。这样,我可以更好地控制图像及其属性。当为uicontrolStateHighlighted设置单独的图像时,在图像视图上将内容模式设置为“缩放到填充”,在按钮上将h&v对齐设置为“填充”时,这似乎不起作用。在xamarin也有工作。
(lldb) po button.imageView
<UIImageView: 0x7faac219a5c0; frame = (0 0; 375 275); clipsToBounds = YES; opaque = NO; userInteractionEnabled = NO; layer = <CALayer: 0x7faac219a6c0>>
[[btn imageView] setContentMode: UIViewContentModeScaleAspectFit];