开发者

How is a UIButton's image set programmatically?

开发者 https://www.devze.com 2023-01-15 02:34 出处:网络
How does one set the image of a开发者_开发百科 UIButton from within the code? I try the following but it just leaves the button as a white square.

How does one set the image of a开发者_开发百科 UIButton from within the code?

I try the following but it just leaves the button as a white square.

[A1 setImage:[UIImage imageNamed:[NSString stringWithFormat:@"m1.10001.png"]] forState:UIControlStateNormal];


Your image construction seems a bit overly complicated, but it should work. Don't forget to use the correct button type (custom):

UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
[button setImage:[UIImage imageNamed:@"m1.10001.png"] forState:UIControlStateNormal];

With newer SDKs you can omit the .png. Make sure that the image actually exists (you can store it in a temporary variable and check for nil, for example).

And of course make sure that the button exists (or is bound), i.e. non-nil, when setting the image.

0

精彩评论

暂无评论...
验证码 换一张
取 消