如何使程序化创建的UIButton中的图像不被缩放?

时间:2011-03-02 13:56:19

标签: uibutton

我正在从UIImages的NSArray中创建一系列UIButton,其中包含以下内容:

#define BUTTONWIDTH 66

    for (int i = 0; i < [imgArray count]; i++){
        toolTile = [UIButton buttonWithType:UIButtonTypeRoundedRect];
        buttonRect = CGRectMake((BUTTONWIDTH*i)+i, 0, BUTTONWIDTH, BUTTONWIDTH);
        toolTile.frame = buttonRect;
        toolTile.tag = i;
        [toolTile addTarget:self action:@selector(toolTileClick:) forControlEvents:UIControlEventTouchUpInside];
        toolTile.imageView.contentMode = UIViewContentModeCenter;
        [toolTile setBackgroundImage:[imgArray objectAtIndex:i] forState:UIControlStateNormal];
        [[self view] addSubview:toolTile];
    }

不幸的是,图像正在缩放(炸毁)以填充66x66图块。我希望它们在瓷砖中以1:1为中心 - 据说toolTile.imageView.contentMode = UIViewContentModeCenter应该这样做。在setBackgroundImage之后,我尝试过移动这一行: - 没有运气。我错过了什么?

1 个答案:

答案 0 :(得分:2)

罪魁祸首是使用setBackgroundImage:forState:[UIControlState]代替setImge:forState:[UIControlState]

SetBackgroundImage显然一直在扩展,SetImage:尊重imageView.contentMode