简体   繁体   English

UIButton backgroundImage与图像

[英]UIButton backgroundImage versus image

I have UIButtons that I was trying to set the images to aspectFit. 我有试图将图像设置为AspectFit的UIButtons。 However using Storyboards I ran into some difficulty I subclassed UIButton and programatically set the imageViews of the buttons to ScaleAspectFit 但是,使用情节提要板时遇到了一些困难,我将UIButton子类化,并以编程方式将按钮的imageViews设置为ScaleAspectFit

#import "UIButtonWithImageAspectFit.h"

@implementation UIButtonWithImageAspectFit

-(void)awakeFromNib{
    [self.imageView setContentMode:UIViewContentModeScaleAspectFit];
}

@end

This worked well and the buttons images display nicely. 这样效果很好,按钮图像显示效果很好。

However, now I have a need to swap out the images of the button programatically so I tried: 但是,现在我需要以编程方式交换按钮的图像,所以我尝试了:

[_firstLetterButton setImage:[UIImage imageNamed:@"2CeadLitir.png"]];

But this does not work so according to advise here I must use: 但这不起作用,因此根据这里的建议我必须使用:

[_firstLetterButton setBackgroundImage:[UIImage imageNamed:@"2CeadLitir.png"] forState:UIControlStateNormal];

which adds the image as a backgroundImage to the button. 这会将图像作为backgroundImage添加到按钮。 Now I have to images in the button. 现在,我必须在按钮中显示图像。

  1. that is scaled nicely as an image property of the button, 可以很好地缩放为按钮的图片属性,
  2. and another that is not scaled nicely as a background image displayed behind. 另一个不能很好地缩放为后面显示的背景图像。

So I returned to the subclass of UIButton UIButtonWithImageAspectFit and tried to change it so that it sets the contentMode of the backgroundImage rather than the image 因此,我返回了UIButton UIButtonWithImageAspectFit的子类,并尝试对其进行更改,以便它设置backgroundImage的contentMode而不是image

But it does not seem to have a backgroundImage property that I can access but according to the documentation it does have 但是它似乎没有我可以访问的backgroundImage属性,但是根据文档,它确实具有

currentBackgroundImage
 Property

However I cannot use the same method setContentMode on that currentBackgroundImage 但是,我不能在该currentBackgroundImage上使用相同的方法setContentMode

Any ideas how to get around this? 任何想法如何解决这个问题?

You need to consider this illustration from Apple web-site. 您需要考虑从Apple网站上获得的插图。 在此处输入图片说明

It's from Buttons tutorial . 它来自Buttons教程 In particular this: 特别是:

State
A button has four states to configure for appearance—default, highlighted, selected, and disabled. 一个按钮具有四种状态来配置外观-默认,突出显示,选中和禁用。 And form 和形式

UIButton class reference UIButton类参考

Discussion 讨论区
In general, if a property is not specified for a state, the default is to use the UIControlStateNormal value. 通常,如果未为状态指定属性,则默认值为使用UIControlStateNormal值。 If the UIControlStateNormal value is not set, then the property defaults to a system value. 如果未设置UIControlStateNormal值,则该属性默认为系统值。 Therefore, at a minimum, you should set the value for the normal state. 因此,至少应设置正常状态的值。

And here is information about different UIControlState: 以下是有关不同UIControlState的信息:

All this links is must-read for beginner iOS Developer. 对于初学者iOS开发人员,必须阅读所有这些链接。

Actually the UIButton has a method 实际上,UIButton有一个方法

- (void)setImage:(UIImage *)image
        forState:(UIControlState)state

so All I needed to do was set the state when setting the image on the UIButton instead of on the UIButton.imageView 所以我要做的就是在UIButton而不是UIButton.imageView上设置图像时设置状态

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM