简体   繁体   English

如何设置UIButton的默认状态图像?

[英]How to set the default-state Image for UIButton?

i am totally confused. 我完全糊涂了。 I just want to set an Image for the Default-State of a list of UIButtons. 我只想为UIButtons列表的默认状态设置一个Image。 But in does not work. 但是在行不通。 I simply assigned the image to all my Buttons inside the setter of my Outlet-Collection but the Image just appears if the Button is highlighted but I want that the Image always is set except the button is selected. 我只是将图像分配给我的Outlet-Collection的setter中的所有按钮,但是如果Button突出显示,则只显示Image但是我希望除了选择按钮之外总是设置Image。 This is my code so far : 到目前为止这是我的代码:

-(void)setCardButtons:(NSArray *)cardButtons
{
    UIImage * cardBackImage = [UIImage imageNamed:@"card-back.jpg"];
    for (UIButton * button in cardButtons) {
        [button setImage:cardBackImage forState:UIControlStateNormal|UIControlStateHighlighted];
        [button setImage:nil forState:UIControlStateSelected];
    }
    _cardButtons = cardButtons;
}

I really dont know what i am doing wrong ? 我真的不知道我做错了什么? How can i set the Image of my Default-State-Button without using the IB ? 如何在不使用IB的情况下设置默认状态按钮的图像?

你有没有尝试过:

    [button setImage:cardBackImage forState:UIControlStateNormal];

There are a few states you can use, make sure you set them correctly. 您可以使用几种状态,请确保正确设置它们。 Copy and paste this into Xcode and fill in your images by tabbing through. 将其复制并粘贴到Xcode中,然后通过Tab键填写图像。

Might be worth while refreshing your memory by reading the Apple Doc for this . 通过阅读Apple Doc可以为您的记忆增添乐趣

[button setImage:<# normal image #> forState:UIControlStateNormal];
[button setImage:<# selected image #> forState:UIControlStateSelected];
[button setImage:<# highlighted image #> forState:UIControlStateHighlighted];
[button setImage:<# selected and highlighted image #> UIControlStateSelected | forState:UIControlStateHighlighted];

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

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