gpt4 book ai didi

objective-c - 创建 NSBox 运行时示例

转载 作者:行者123 更新时间:2023-12-03 16:33:08 25 4
gpt4 key购买 nike

请举例说明如何创建 NSBox 实例运行时并将其放置在 NSView 中?

最佳答案

NSView *superview = …; // Reference to the view that will contain the box
// for instance, [window contentView]

NSUInteger resizeAllMask = (NSViewWidthSizable
| NSViewHeightSizable
| NSViewMinXMargin
| NSViewMaxXMargin
| NSViewMinYMargin
| NSViewMaxYMargin);

// This is the box. We use an autoresizing mask so that it occupies
// the entire superview
NSBox *box = [[NSBox alloc] initWithFrame:[superview bounds]];
[box setAutoresizingMask:resizeAllMask];
[box setBoxType:NSBoxPrimary];
[box setBorderType:NSBezelBorder];
[box setTitle:@"This is a box"];

// This is the box' content view. It represents the box contents.
// By default, a box autoresizes its content view so that it occupies
// the entire box
NSView *boxContentView = [[NSView alloc] initWithFrame:NSZeroRect];
[box setContentView:boxContentView];

// For example, we add a text field to the box' content view
NSRect textRect = {{0,0}, {100,20}};
NSTextField *textField = [[NSTextField alloc] initWithFrame:textRect];
[textField setStringValue:@"Hey there"];
[boxContentView addSubview:textField];

[superview addSubview:box];

关于objective-c - 创建 NSBox 运行时示例,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6225085/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com