<分区>
如何以编程方式在 macOS Cocoa 应用程序中创建和定位按钮?
<分区>
如何以编程方式在 macOS Cocoa 应用程序中创建和定位按钮?
最佳答案
定位按钮 你需要改变按钮的原点 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。
关于objective-c - 以编程方式在 macOS 应用程序中创建和定位 NSButton?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10251703/
相关文章:
iphone 开发 : how to set a property of an another viewController then reach it
iOS - 这个应用程序使用什么 Controller 来显示日期和控制向前和向后移动一个日期?
objective-c - 如何关联多个 NSMenuItems 的状态,使它们互斥?
cocoa - 删除 NSManagedObjectContext 中的所有记录
objective-c - 从 Objective-C 访问 Swift Delegate 的正确方法是什么?
objective-c - ARC下EXC_BAD_ACCESS内存错误
java - 如何为我的 java swing 应用程序准备 Mac 安装程序
macos - 如何在 Mac OS 上通过 PID 获取进程用户名?
macos - 用 LET 定义的 Swift 常量有时会在读取时产生 EXC_BAD_ACCESS (EXC_I386_GPFLT) (OSX)