2013-07-26 63 views
1

我有一個問題:我在XIB創建1個NSScollView。然後我編程創建20個NSTextFile並將它們添加到ScrollView的ContentView。但是,當添加多個NStextfield時,scrollview不滾動,許多NStextfield已被刪除,而不是顯示所有NSTextField。 這是我的代碼:NSScrollView不滾動和大小contentView不正確的可可

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。你可以幫我嗎?

+0

你嘗試到幀設爲內容查看?嘗試像這樣:contentView.frame = NSMakeRect(0,0,1.0e7,1.0e7); – stosha

+0

我嘗試添加你的代碼,它在上面有空格,NSTextFiels在最下面。我可以如何正確地把NSTextField的編程位置放置@stosha – HTKT611

+0

嘗試插入contentView.frame = NSMakeRect(0,0,1.0e7,number * 30);之前[ScrollView setDocumentView:contentView]; – stosha

回答

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]; 
}