Dynamically resize NSWindow based on NSOutlineView height

喜欢而已 提交于 2019-12-06 06:40:56

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.

One approach is to record the initial height of the outline view and the initial height of the window and then, as the outline view height changes, compute the new height of the window = old height + (new outline height - old outline height). By working only in terms of changes from an initial state, it makes things more flexible. You don't have to replicate your whole layout in code as you would if you were recomputing the size of things from scratch. You can use IB to lay out the window and it should retain that layout without the code having to know much about the specifics. And you can update the layout in IB and the code still works.

Otherwise, I suggest you show what you've tried and explain how it isn't right.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!