0

我创建了一个 NSScrollView 并在其中添加了自定义 NSVIew,但是在运行时,它始终在底部有一个空间,并且滚动条始终在底部。现在,我想滚动视图与自定义视图和滚动条始终位于顶部。这是我的代码:

 float label_Y;
float textfield_Y;
float heightView;
-(void) createTableWiFi: (int) number
{

    heightView = (25 + 30) * number;

    guiView = [[NSView alloc] initWithFrame:NSMakeRect(0,0,600, heightView)];

    label_Y = heightView;
    textfield_Y = heightView ;
    [[ScrollView verticalScroller] setFloatValue:0.0];
    [[ScrollView contentView] scrollToPoint:NSMakePoint(0.0, heightView)];
    int i;
    for(i=1; i<=number;i++)
    {

        ssidtxt = [[NSTextField alloc] initWithFrame:NSMakeRect (50,textfield_Y,130,25)];
        [ssidtxt setBezelStyle:NSTextFieldSquareBezel];
        ssidtxt.tag=i;
        [ssidtxt setAutoresizingMask:NSViewWidthSizable];

        [guiView addSubview:ssidtxt];

        [ssidtxt release];


        keytxt = [[KSPasswordField alloc] initWithFrame:NSMakeRect (200,textfield_Y,130,25)];
        [guiView addSubview:keytxt];
        [keytxt release];

        textfield_Y -=30;
    }
    prototype= [[NSButtonCell alloc] init];
    [prototype setTitle:@""];
    [prototype setButtonType:NSRadioButton];

    NSRect matrixRect = NSMakeRect(500, textfield_Y +25  , 50,25 *(i-1));
    myMatrix = [[NSMatrix alloc] initWithFrame:matrixRect
                                          mode:NSRadioModeMatrix
                                     prototype:(NSCell *)prototype
                                  numberOfRows:i-1
                               numberOfColumns:1];
    [myMatrix setCellSize:NSMakeSize(50, 30)];

    [myMatrix setAction:@selector(radioButtonClicked:)];
    NSArray *cellArray = [myMatrix cells];

    [guiView addSubview:myMatrix];

    [ScrollView  setDocumentView :guiView];
    [[ScrollView verticalScroller] setFloatValue:0.0];
    [[ScrollView contentView] scrollToPoint:NSMakePoint(0.0, heightView)];
    [guiView release];
}

欢迎任何建议。提前致谢

4

1 回答 1

1

不要这样做。

[[ScrollView verticalScroller] setFloatValue:0.0];

你可能会强迫它导致一些不一致。如果您的滚动条位置没有更新(我不太确定这是否是您的问题)使用

[ScrollView reflectScrolledClipView:[ScrollView contentView]];

另请阅读Cocoa 编码指南。这是一个相对较短的阅读,但非常值得。

于 2013-11-01T21:25:12.107 回答