以编程方式在macOS应用中创建和放置NSButton? [英] Programmatically create and position an NSButton in a macOS app?
本文介绍了以编程方式在macOS应用中创建和放置NSButton?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
如何以编程方式在macOS Cocoa应用程序中创建和放置按钮?
How do I programmatically create and position a button in a macOS Cocoa application?
推荐答案
到位置按钮您需要更改按钮的原点x和y.看看我在下面编写的示例代码和注释.
To possition button You need to change the button's origins x and y. Look at the sample code which I wrote below and comments.
您可以这样做:
-(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.
** WindowOutlet is window so don't forget to IBOutlet it.
这篇关于以编程方式在macOS应用中创建和放置NSButton?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文