首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >创建NSBox运行时示例

创建NSBox运行时示例
EN

Stack Overflow用户
提问于 2011-06-03 17:07:13
回答 1查看 2.5K关注 0票数 2

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

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2011-06-03 17:52:57

代码语言:javascript
复制
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];
票数 3
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/6225085

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档