NSScrollView 中的 NSView 未正确调整大小(尝试以编程方式调整 DocumentView 的大小)



ADDITION #2我还尝试使用以下代码行来获取大小 contSizeW 和 contSizeWO:

NSSize contSizeW  = [NSScrollView contentSizeForFrameSize:self.topLevelScroller.frame.size
                                  horizontalScrollerClass:[self.topLevelScroller.horizontalScroller class]
                                    verticalScrollerClass:nil
                                               borderType:self.topLevelScroller.borderType
                                              controlSize:NSRegularControlSize
                                            scrollerStyle:NSScrollerStyleOverlay];
NSSize contSizeWO = [NSScrollView contentSizeForFrameSize:self.topLevelScroller.frame.size
                                  horizontalScrollerClass:nil
                                    verticalScrollerClass:nil
                                               borderType:self.topLevelScroller.borderType
                                              controlSize:NSRegularControlSize
                                            scrollerStyle:NSScrollerStyleOverlay];

上述 2 种大小应该不同,因为将水平 ScrollerClass 设置为 nil ,通过苹果文档,这意味着它应该假设滚动条不可见,并且通过提供非 nil,应该假设它正在显示滚动条。

在这种情况下,无论我为水平滚动类输入什么,它总是返回相同的高度。

编辑和添加 #1

-(void)initContentView{
    self.content = [[NSView alloc] init];
    [self.content setFrame:NSMakeRect(1, 1, 
                                      [[BubbleSharedPathObject sharedPath] itemsInPathList] * FILE_LIST_COLUMN_WIDTH, 
                                      self.bounds.size.height - 2)];
    [self.content setAutoresizingMask:NSViewMinXMargin | NSViewMinYMargin |
                                      NSViewMaxYMargin | NSViewHeightSizable];
    [self.content setAutoresizesSubviews:YES];
    [self.topLevelScroller setDocumentView:self.content];

}

源语言我有一个自定义视图,我正在尝试以编程方式调整大小。 我的 NSScrollView 设置为只有一个水平滚动条,而没有垂直滚动条。 滚动视图还设置为自动隐藏滚动条。 我有一个例程叫做:

-(void)addPathComponentToEnd:(NSString *)pathComp

此例程只会增加包含内容的自定义视图的宽度,并且是 ScrollView 的文档视图。 有趣的是,不起作用的代码片段是:

[self.topLevelScroller setAutoresizesSubviews:NO];
[self.content setAutoresizesSubviews:NO];
NSSize contSizeW  = [NSScrollView contentSizeForFrameSize:self.topLevelScroller.frame.size hasHorizontalScroller:YES hasVerticalScroller:NO borderType:self.topLevelScroller.borderType];
NSSize contSizeWO = [NSScrollView contentSizeForFrameSize:self.topLevelScroller.frame.size hasHorizontalScroller:NO hasVerticalScroller:NO borderType:self.topLevelScroller.borderType];
NSLog(@"Size With = (%d, %d), Size Without = (%d, %d)", (unsigned int)contSizeW.width, (unsigned int)contSizeW.height, (unsigned int)contSizeWO.width, (unsigned int)contSizeWO.height);
// Resize the content view to handle the added list view
if( ( [[BubbleSharedPathObject sharedPath] itemsInPathList] * FILE_LIST_COLUMN_WIDTH ) > contSizeW.width ){
    [self.content setFrameSize:NSMakeSize([[BubbleSharedPathObject sharedPath] itemsInPathList] * FILE_LIST_COLUMN_WIDTH + 2, contSizeW.height)];
    for(FileListTableView * table in self.tablesList){
        //[table setFrame:NSMakeRect(table.fileList.tag * FILE_LIST_COLUMN_WIDTH, 0, FILE_LIST_COLUMN_WIDTH, self.content.frame.size.height)];
        [table setFrameSize:NSMakeSize(FILE_LIST_COLUMN_WIDTH, contSizeW.height)];
        NSLog(@"file list table height = %d", (unsigned int)table.frame.size.height);
    }
    NSLog(@"New Rect (x, y, w, h) = (%d, %d, %d, %d)", (unsigned int)self.content.frame.origin.x, (unsigned int)self.content.frame.origin.y, (unsigned int)self.content.frame.size.width, (unsigned int)self.content.frame.size.height);
}else{
    [self.content setFrame:NSMakeRect(1, 1, [[BubbleSharedPathObject sharedPath] itemsInPathList] * FILE_LIST_COLUMN_WIDTH + 2, contSizeWO.height)];
    //[self.content setFrame:NSMakeRect(1, 1, [[BubbleSharedPathObject sharedPath] itemsInPathList] * FILE_LIST_COLUMN_WIDTH, self.frame.size.height - 2)];
    NSLog(@"New Rect (x, y, w, h) = (%d, %d, %d, %d)", (unsigned int)self.content.frame.origin.x, (unsigned int)self.content.frame.origin.y, (unsigned int)self.content.frame.size.width, (unsigned int)self.content.frame.size.height);
}

包含此代码的子例程进行多次调用的日志记录的输出如下所示:

Size With = (501, 206), Size Without = (501, 221)
New Rect (x, y, w, h) = (1, 1, 402, 221)
Size With = (501, 206), Size Without = (501, 221)
file list table height = 206
file list table height = 206
New Rect (x, y, w, h) = (1, 0, 602, 192)
Size With = (501, 206), Size Without = (501, 221)
file list table height = 206
file list table height = 206
file list table height = 206
New Rect (x, y, w, h) = (1, 0, 802, 206)

代码:

[[BubbleSharedPathObject sharedPath] itemsInPathList]
最初只返回 1,

所以所有后续调用都会添加 1,因此应该(我已经验证过)它返回 1,然后返回 2、3、4、5、6 等。

请注意,从日志记录中,代码第一次通过"if"(相对于else)时,日志记录显示self.content视图没有采用它应该的高度值206,并且以某种方式提出了自己的值192。 另请注意,在该调用之后(并在之后验证了所有其他调用)实际上最终确实采用了 206 高度的正确值。

问题:为什么视图没有采用我分配给它的大小? 以前有没有人见过这种行为,你是怎么解决的?

提前非常感谢。

根据您的解释,我想我理解您的例程,您首先获得 192 然后再次获得 206 的日志确实很奇怪。但是,(unsigned int)self.content.frame.size.height 由 contSizeW 设置,它再次由已弃用的方法设置内容大小为框架大小:有水平滚动条:有垂直滚动条:边框类型:

根据 Xcode 6.1.1 文档和 API 参考,此方法自 OS X 10.7 起已弃用,您应该使用:contentSizeForFrameSize:horizontalScrollerClass:verticalScrollerClass:borderType:controlSize:scrollerStyle: 代替。它还指出,对于现有视图,您可以简单地使用 contentSize 方法。

您能否使用这些方法中的任何一种,而不是已弃用的方法,看看问题是否已解决。如果这没有帮助,我和你一样被困住了。

下面是有关该方法的文档。很抱歉无法为您提供简单的解决方案。

内容大小框架大小大小:水平滚动条类

:垂直滚动条类:边框类型:控件大小:滚动条样式: 返回根据帧大小计算的内容大小和 指定的规格。

声明 SWIFT 类函数内容大小为帧大小

(_ 帧大小: NSSize, 水平滚动类 水平滚动类: 任何类?, verticalScrollerClass verticalScrollerClass: AnyClass?, borderType borderType: NSBorderType, 控制大小控制大小: NSC控制大小, scrollerStyle scrollerStyle: NSScrollerStyle) -> NSSize OBJECTIVE-C + (NSSize)contentSizeForFrameSize:(NSSize)frameSize 水平滚动类:(类)水平滚动类 verticalScrollerClass:(Class)verticalScrollerClass borderType:(NSBorderType)borderType 控制大小:(NSC控制大小)控制大小 scrollerStyle:(NSScrollerStyle)scrollerStyle Parameters frameSize 屏幕坐标中的帧大小。 水平滚动类 用作水平滚动条的类。一个 值 nil 指定不使用水平滚动条。 verticalScrollerClass 用作垂直滚动器的类。一个 值 nil 指定不使用水平滚动条。 borderType 指定滚动视图样式的外观 边境。有关可能值的列表,请参阅 NSBorderType。控件大小
控件大小。可能的值在 NSControlSize 中指定。 不支持 NSMiniControlSize。scrollerStyle 指定 滚动样式。有关支持的值,请参阅 NSScrollerStyle。返回值 内容视图帧大小。

讨论 对于现有的滚动视图,您只需使用 内容大小方法。

导入

语句导入应用套件

可用性在 OS X v10.7 及更高版本中可用。

最新更新