Programatically create and position an NSButton in an OS X app? [closed]

北战南征 提交于 2019-12-04 07:48:34

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.

You can do it like this:

-(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 is window so don't forget to IBOutlet it.

All buttons are controls and all controls are views, so see, in order:

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!