1

我需要一个类似于 IB 的 Inspectors 的布局,其中有多个可扩展部分,由显示三角形扩展,所有这些都包含在滚动视图中。

如果只需要一个可扩展部分,我已经在那里了:我把可扩展部分放在一个 NSBox 中,给盒子和它上面的所有东西一个顶部支柱但没有底部支柱,并给它下面的所有东西一个底部支柱但没有顶部支撑。然后我设置了显示三角形的动作来显示/隐藏框并调整滚动视图的文档视图的框架大小。

但似乎没有办法为多个盒子设置支柱。关闭披露三角形会留下间隙,或者盒子会相互重叠。

我确实看过 NSOutlineView,但那是一张表格;它不能有像组合框和按钮这样的子视图。(或者也许可以,如果我制作自定义单元格,这是我还没有做过的事情——但我怀疑这些不适合全功能布局。)

有人可以指出我正确的方向吗?

4

2 回答 2

2

万一其他人遇到这个设计挑战,我会发布我想出的 IBAction。

该方案使用常规的、未翻转的视图。也就是说,原点在左下角。更改 docSize 时,会从顶部添加或删除空间。

虽然对于单个显示三角形,一些控件需要顶部支柱,有些需要底部支柱,但对于此方案,所有控件必须同时具有顶部和底部支柱。否则他们会自动调整自己,抛弃一切。

如最后所述,当完全滚动到底部时会遇到相当大的挑战。但那是另一章……</p>

/**
 Action called upon clicking a disclosure triangle.
 Hides or discloses the box associated with the disclosure triangle.
 */
- (IBAction) discloseBox:(id)sender {

// Determine which box is governed by this disclosure triangle.
NSBox *boxTarget;
switch ([sender tag]) {
    case kDT_Source:
        boxTarget = self.boxSourceInfo;
        break;
    case kDT_Tweak:
        boxTarget = self.boxTweak;
        break;
    case kDT_Series:
        boxTarget = self.boxSeries;
        break;
    case kDT_Abbrevs:
        boxTarget = self.boxAbbreviations;
        break;
    case kDT_Flag:
        boxTarget = self.boxFlaggingAndComments;
        break;
    default:
        break;
}

// Get size info on the content with and without the box.
NSView *docView = [self.svEditorMain documentView];
NSSize docSize = [docView frame].size;
CGFloat fHeightChange = [boxTarget frame].size.height;

// Before actually changing the content size, record what point is currently at the top of the window.
CGFloat dropFromTop_preChange = [self getCurrentDropFromTop];

// If the state is now on, make the box visible.
// If the state is now off, hide the box and make the height change negative.
switch ([sender state]) {
    case NSOnState:
        [boxTarget setHidden:NO];
        break;
    case NSOffState:
        [boxTarget setHidden:YES];
        fHeightChange *= -1;
        break;
    default:
        break;
}
// Use the height change to prepare the adjusted docSize, but don't apply it yet.
NSSize adjustedDocSize = NSMakeSize(docSize.width, (docSize.height + fHeightChange));

// Make sure the adjustees array is populated.
[self populateVerticalAdjusteesArray];

// If the height change is positive, expand the content size before adjusting the origins, so that the origins will have space to move up into. (Space will be added at top.)
if (fHeightChange > 0)
    [docView setFrameSize:adjustedDocSize];

// Get the current, pre-change Y origin of the target box.
CGFloat boxOriginY_preChange = [boxTarget frame].origin.y;
// Loop through the adjustees, adjusting their height.
NSControl *control;
CGFloat originX;
CGFloat originY;

for (NSUInteger ui = 0; ui < [self.carrVerticalAdjustees count]; ++ui) {
    control = [self.carrVerticalAdjustees objectAtIndex:ui];
    originY = [control frame].origin.y;
    // Adjust all controls that are above the origin Y of the target box (but do nothing to the target box itself).
    // Since coordinate system places origin at lower left corner, higher numbers are higher controls.
    if (originY > boxOriginY_preChange) {
        originX = [control frame].origin.x; // get originX just so you can assemble a new NSPoint
        originY += fHeightChange; 
        [control setFrameOrigin:NSMakePoint(originX, originY)];
    }
    // Since the array was assembled in order from top down, once a member is encountered whose origin is below the target's, we're done.
    else 
        break;
}

// If the height change is negative, contract the content size only now, after the origins have all been safely adjusted downwards. (Space will be removed at top.)
if (fHeightChange < 0)
    [docView setFrameSize:adjustedDocSize];

// Left to its own devices, the scroller will maintain the old distance from the bottom, so whatever is under the cursor will jump up or down. To prevent this, scroll the content to maintain the old distance from the TOP, as recorded above.
// (This won't work if user is already scrolled to the bottom and then collapses a box. The only way to maintain the scroll position then would be to add blank space at the bottom, which would require moving  the origin of all the content up. And then you would want to reverse those changes as soon as the blank space scrolled back out of view, which would require adding some trackers and monitoring NSViewBoundsDidChangeNotification.)
[self scrollMainTo:dropFromTop_preChange];  

}

于 2011-03-25T13:40:19.093 回答
0

查看InspectorKit。但是,如果您使用的是 Xcode 4,请记住它不再支持 IBPlugins,因此您必须在代码中使用 InspectorKit(并且不使用 Interface Builder 插件的拖放便利)。

于 2011-03-23T16:14:58.823 回答