根据 NSOutlineView 高度动态调整 NSWindow 的大小 [英] Dynamically resize NSWindow based on NSOutlineView height

查看:34
本文介绍了根据 NSOutlineView 高度动态调整 NSWindow 的大小的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经阅读了许多关于动态调整 NSWindows 大小的问题的答案,但还没有完全奏效.

我已经构建了一个类似popover"的窗口以从菜单栏显示,我无法使用 NSPopover,因为它在设计方面不够可定制.我的视图层次结构当前看起来像这样:

NSWindow 子类- NSView(清除标题栏渲染,绘制弹出箭头)- NSView(内容视图)- NSOutlineView(主目录)- NSView(窗口页脚)

我需要的是让窗口随着 NSOutlineView 中项目的扩展和收缩而扩展和收缩,以便窗口始终准确地用于大纲视图、页脚和顶部弹出箭头的正确高度.

我有一些方法可以根据内容计算大纲视图所需的高度.到目前为止,我一直在尝试在内容视图控制器上调用 awakeFromNib 时重新计算大小,然后再次在委托方法 outlineViewItemDidCollapse:outlineViewItemDidExpand:,但我永远无法正确调整窗口大小.

我尝试了很多不同的方法,但都没有奏效.有没有一种规范"的方式来做到这一点?我已经看到人们谈论 -[NSWindow frameForContentRect:] 与此类问题有关,但我不太清楚这是如何需要的.

也许我的处理方式完全错误,但我希望不是.看起来这应该是可能的,它只是将正确的东西放在正确的地方.不幸的是,GUI 编程对我来说并不是一个强项.我很感激人们的任何想法或解决方案.

谢谢.

这已解决,部分原因是标记的答案,部分原因是其他一些事情.清除 NSView 没有自动调整大小掩码,因此我将其设置为向所有方向扩展,这有助于解决一些调整大小问题.此外,我用于计算所需高度的方法并不完全正确,并且存在一些问题.

根据提供的答案,找到所需高度的方法在某些情况下是好的,但我没有可以测量的根"项目.这是可能需要考虑的事情.

解决方案

这是我的方法,效果很好,但可能还有比这更好的方法.

- (void)outlineViewItemDidExpand:(NSNotification *)notification {[自行调整窗口高度];}- (void)outlineViewItemDidCollapse:(NSNotification *)notification {[自行调整窗口高度];}- (void)resizeWindowHeight {NSRect wRect = [myWindow 框架];NSRect oRect = [myoutlineView rectOfRow:([myoutlineView numberOfRows] - 1)];//获取最后一行的矩形CGFloat n = oRect.origin.y + oRect.size.height + 22;wRect.origin.y = wRect.origin.y + (wRect.size.height - n);wRect.size.height = n;[myWindow setFrame:wRect display:YES animate:YES];}

数字22是:(顶部窗口-顶部轮廓)+轮廓标题高度+水平滚动条高度+(底部窗口-底部轮廓)+工具栏高度的总和

我在 IB 中的设置对于这个例子:没有水平滚动条和标题,Outline 的高度 = 窗口的 contentView 的高度,所以 22 是窗口边框.

如果你有工具栏或者水平滚动条自动隐藏,你需要在代码中添加条件来检查(工具栏和滚动条)的可见性并相应地改变高度.

I have read many answers to questions about dynamically resizing NSWindows and nothing has quite worked yet.

I have built a 'popover' like window to be displayed form the menu bar, I couldn't use NSPopover because it isn't quite customisable enough in terms of design. My view hierarchy current looks like this:

NSWindow Subclass
  - NSView (clears the titlebar rendering, draws popover arrow)
     - NSView (contentView)
        - NSOutlineView (main table of content)
        - NSView (window footer)

What I need is for the window to expand and contract with items in the NSOutlineView expanding and contracting, so that the window is always precisely the correct height for the outline view, footer, and the popover arrow at the top.

I have methods to calculate the required height for the outline view based on the content. So far I have been trying to recalculate the size when awakeFromNib is called on the content view controller, and then again on the delegate methods outlineViewItemDidCollapse: and outlineViewItemDidExpand:, but I am never able to resize the window correctly.

I have tried lots of different ways and none worked. Is there a 'canonical' way to do this? I've seen people talking about -[NSWindow frameForContentRect:] in relation to this sort of problem, but I can't quite work out how that is needed.

Maybe I am going about this completely the wrong way, I hope not though. It seems like this should be possible, it's just getting the right things in the right places. Unfortunately GUI programming is not a strong point for me. I would appreciate any ideas or solutions people have.

Thanks.

Edit: This was solved, partly due to the marked answer, and partly due to a few other things. The clearing NSView did not have an autoresize mask, so I set this to expand in all directions, this helped with some of the resizing issues. Also, the method I was using for calculating the height required was not entirely correct and had a few problems.

With the provided answer, the way to find the required height is good for some circumstances, but I didn't have a 'root' item that could be measured. This is something that may need to be taken into account.

解决方案

Here is my method, it works very well, but there are probably better than that.

- (void)outlineViewItemDidExpand:(NSNotification *)notification {
    [self resizeWindowHeight];
}
- (void)outlineViewItemDidCollapse:(NSNotification *)notification {
    [self resizeWindowHeight];
}
- (void)resizeWindowHeight {
    NSRect wRect = [myWindow frame];
    NSRect oRect = [myoutlineView rectOfRow:([myoutlineView numberOfRows] - 1)]; //get rect of last row
    CGFloat n = oRect.origin.y + oRect.size.height + 22;
    wRect.origin.y = wRect.origin.y + (wRect.size.height - n);
    wRect.size.height = n;
    [myWindow setFrame:wRect display:YES animate:YES];
}

The number 22 is the sum of : (top window - top Outline) + height of Outline header + height of horizontal scroller + (bottom window - bottom Outline) + height of toolbar

My settings in IB for this example : no horizontal scroller and no header , height of Outline = height of the contentView of the window, so 22 is the window border.

If you have a toolbar or the horizontal scroller is hidden automatically, you need to add conditions in the code to check the visibility of (the toolbar and scroller) and change the height accordingly.

这篇关于根据 NSOutlineView 高度动态调整 NSWindow 的大小的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆