如何将图像设置为以编程方式创建的按钮

时间:2013-01-21 08:15:31

标签: objective-c cocoa

  

可能重复:
  Using -setImage on NSButton

I created a button using following code .


    NSView *superview = [_window contentView];
    NSRect frame = NSMakeRect(10, 10, 200, 100);
    button = [[NSButton alloc] initWithFrame:frame];
    [button setTitle:@"Click me!"];

那么如何将图像设置为此按钮?

2 个答案:

答案 0 :(得分:2)

[button setBackgroundImage:[UIImage imageNamed:@"yourImage"] forState:UIControlStateNormal];

答案 1 :(得分:2)

您应该使用:

- (void)setImage:(NSImage *)anImage

e.g:

[button setImage:[NSImage imageNamed:@"image"]];
相关问题