简体   繁体   中英

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. 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. 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 ?

你有没有尝试过:

    [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.

Might be worth while refreshing your memory by reading the Apple Doc for this .

[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];

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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