MNY
MNY

Reputation: 1536

Cant resize an UIImage properly

I have an updateUI method for playing cards game and I added an image for the back of the card but the picture is all over the place and I cant fint the problem..

This is my method: how do i resize it to to match the card size?

- (void)updateUI {

    for (UIButton *cardButton in self.cardButtons) {
        Card *card = [self.game cardAtIndex:[self.cardButtons indexOfObject:cardButton]];
        [cardButton setTitle:card.contents forState:UIControlStateSelected];
        [cardButton setTitle:card.contents
                    forState:UIControlStateSelected|UIControlStateDisabled];
        cardButton.selected = card.isFaceUp;
        cardButton.enabled = !card.isUnplayable;
        cardButton.alpha = card.isUnplayable ? 0.3 : 1.0;

        cardButton.imageEdgeInsets = UIEdgeInsetsMake(2, 2, 2, 2);
        UIImage *cardBackImage = (cardButton.selected) ? nil : [UIImage imageNamed:@"benCardImag.png"];
        [cardButton setImage:cardBackImage forState:UIControlStateNormal];

    }

    self.scoreCounter.text = [NSString stringWithFormat:@"Score: %d", self.game.score];

    if (self.game.notification) {

        self.notificationLabel.text = self.game.notification;

    }
}

Upvotes: 0

Views: 55

Answers (1)

Alejandro
Alejandro

Reputation: 3746

Use setBackgroundImage:forState: instead of setImage:forState:. The image is adjusted to the left of the text, but the background image is scaled to fill the button. You may also want to try a custom UIControl instead of an UIButton if you want to control the placement of the images and text.

Upvotes: 1

Related Questions