如何以编程方式在 macOS Cocoa 应用程序中创建和定位按钮?
问问题
18908 次
2 回答
19
定位按钮 您需要更改按钮的原点 x 和 y。查看我在下面编写的示例代码和注释。
你可以这样做:
-(void)awakeFromNib {
//Start from bottom left corner
int x = 100; //possition x
int y = 100; //possition y
int width = 130;
int height = 40;
NSButton *myButton = [[[NSButton alloc] initWithFrame:NSMakeRect(x, y, width, height)] autorelease];
[[windowOutlet contentView] addSubview: myButton];
[myButton setTitle: @"Button title!"];
[myButton setButtonType:NSMomentaryLightButton]; //Set what type button You want
[myButton setBezelStyle:NSRoundedBezelStyle]; //Set what style You want
[myButton setTarget:self];
[myButton setAction:@selector(buttonPressed)];
}
-(void)buttonPressed {
NSLog(@"Button pressed!");
//Do what You want here...
}
** WindowOutlet是窗口所以不要忘记 IBOutlet 它。
于 2012-04-20T19:48:55.977 回答