NSScrollView不能在Cocoa中滚动和调整大小内容

时间:2013-07-26 09:50:43

标签: objective-c macos cocoa nstextfield nsscrollview

我遇到了问题:我在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的位置不正确。它似乎不是autoresize contentView适合ScrollView。你能救我吗?

1 个答案:

答案 0 :(得分: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];
}