0

我有一个用于玩纸牌游戏的 updateUI 方法,我在卡片背面添加了一张图片,但图片到处都是,我找不到问题..

这是我的方法:我如何调整它以匹配卡片大小?

- (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;

    }
}
4

1 回答 1

1

使用setBackgroundImage:forState:而不是setImage:forState:. 图像被调整到文本的左侧,但背景图像被缩放以填充按钮。如果您想控制图像和文本的位置,您可能还想尝试自定义 UIControl 而不是 UIButton。

于 2013-03-15T23:17:55.387 回答