1

我有一个问题:我在 XIB 中创建了 1 个 NSScollView。然后我以编程方式创建 20 个 NSTextFile 并将它们添加到 ScrollView 的 ContentView。但是当添加多个 NStextfield 时,scrollview 不滚动并且许多 NStextfield 已被删除,不显示所有 NSTextField。这是我的代码:

IBOutlet NSView *contentView;
IBOutlet NSScrollView *ScrollView;

函数 createDynamictextField:

-(void) createLabelDynamic : (int) number andX: (int) x andY : (int) y
{
 for(int i=1; i<=number;i++)
     {
         NSTextField *ssid = [[NSTextField alloc] initWithFrame:NSMakeRect (x,y,150,100)];
         [ssid setStringValue:[NSString stringWithFormat:@"SSID %d :",i]];
         [ssid setSelectable:NO];
         [ssid setEditable:NO];
         [ssid setBordered:NO];
         [ssid setDrawsBackground:NO];
         [ssid setAutoresizingMask:NSViewWidthSizable];
         [contentView addSubview:ssid];
         y -=30;
         [ssid release];
     }
    [ScrollView  setDocumentView :contentView];
}

如果 NStextField 较少,例如 2 个 NSTextFiels,则 NSTextField 的位置不正确。它似乎不自动调整 contentView 适合 ScrollView。你能帮助我吗?

4

1 回答 1

1

试试这样:

-(void) createLabelDynamic : (int) number andX: (int) x andY : (int) y
{
     for(int i=1; i<=number;i++)
     {
         NSTextField *ssid = [[NSTextField alloc] initWithFrame:NSMakeRect (x,y,150,30)];
         [ssid setStringValue:[NSString stringWithFormat:@"SSID %d :",i]];
         [ssid setSelectable:NO];
         [ssid setEditable:NO];
         [ssid setBordered:NO];
         [ssid setDrawsBackground:NO];
         [ssid setAutoresizingMask:NSViewWidthSizable];
         [contentView addSubview:ssid];
         y -=30;
         [ssid release];
     }
    contentView.frame = NSMakeRect(0, 0, 150, y + number * 30);
    [ScrollView  setDocumentView :contentView];
}
于 2013-07-26T10:47:39.160 回答