【发布时间】:2015-01-30 13:24:22
【问题描述】:
补充 #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 种尺寸应该不同,因为其中一个将 HorizontalScrollerClass 设置为 nil,苹果文档意味着它应该假定滚动条不可见,并且通过提供非零值,应该假定它显示滚动条。
在这种情况下,无论我为 HorizontalScrollerClass 输入什么,它总是返回相同的高度。
编辑和添加 #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 设置为只有一个水平滚动条,没有垂直滚动条。 ScrollView 也设置为自动隐藏滚动条。我有一个例程叫做:
-(void)addPathComponentToEnd:(NSString *)pathComp
此例程只会为包含内容的自定义视图添加宽度,并且是 ScrollView 的文档视图。有趣的是不工作的代码的 sn-p 是:
[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 高度的正确值。
问题: 为什么视图没有采用我分配给它的大小?以前有没有其他人见过这种行为,你是如何解决的?
非常感谢。
【问题讨论】:
-
@MacUserT - 有没有一个问题与你好:-)
-
嗨 Trumpetlicks,我一直在阅读您的代码并有一些问题。您的 sn-p 从设置 contSizeW 和 contSizeWO 的 NSSize 开始。然后你到达if语句,当我正确阅读你的日志时首先出现错误,因为从我阅读的日志中它进入“else”语句并将self.content的框架设置为1、1、402、221 . 我没看到的是,程序如何返回注释//Resize ...下的if语句,FILE_LIST_COLUMN_WIDTH的值是什么以及contentSizeW和self.content.frame.size之间的关系是什么。我错过了什么吗?
-
@MacUserT - 因此整个例程从外部操作调用,当该外部操作发生时,会更新一组数据。该数据本质上是驱动“[[BubbleSharedPathObject sharedPath] itemsInPathList]”的原因,contSizeW 应该是水平滚动条存在时滚动视图的内容大小能力,而 ContSizeWo 是水平滚动条不存在时滚动视图的大小能力展示。 if 语句用于确定新添加的内容是否比滚动框功能更宽。
-
@MacUserT - 续。从上面。我从不希望内容视图比可见区域可以提供的滚动视图更高,只是更宽。因此,我想要完成的是识别我的内容何时更宽,并考虑在水平滚动条将出现时发生的可见帧丢失。无论如何,我仍然不明白我在计算时如何直接将视图设置为高度 206,正如
ContSizeW所呈现的那样,它不仅没有采用那个数字,而且也没有保持它拥有的那个数字之前,并想出一些全新的东西。 -
嗨 trumpetlicks,谢谢你的帮助。很抱歉一直问但没有给你解决方案。这是我的问题。具有此代码 sn-p 方法的类包含 FileListTableView 类的表数组。第一次列宽大于contSizeW,你把table list里面的table frame size设置为FILE_LIST_COLUMN_WIDTH(我假设是table应该有的指定列宽。设置完所有table的列宽后你登录矩形 self.content 的尺寸。
标签: macos nsview nsscrollview