macosinterface-builderlinensbox

How to add an line of NSBox programmatically?


In interface builder, we can simply drag a line to the view.

But I want to programmatically assign a line (for example, vertical line) into my view hierarchy, what should I initialize the box before - addSubview: and - setFrame: methods?


Solution

  • Sorry, I solve it myself. Here are the codes:

    NSBox *line = [[NSBox alloc] init];
    [line setBoxType:NSBoxSeparator];                     /* make it a line */
    [line setFrame:NSMakeRect(0.0, 20.0, 300.0, 0.0)];    /* Or assign NSLayoutConstraint objects */
    [self.view addSubview:line];