我正在尝试创建一个类似reeder/sparrow的ui来处理我的应用程序的内容。目前我使用一个NSPLISTVIEW,里面有两个NSVIEW(左边的是内容列表,右边的是“检查器”)。
我想知道的是如何在标题栏上创建分隔符,它也可以作为分割视图的分隔符。
我已经在使用INAppStoreWindow子类了。
有什么想法吗?提前付款

最佳答案

我这样做的方法是添加一个nssplitview子类作为inappstorewindow的tilebarview的子视图:

// This code comes from the INAppStoreWindow readme
INAppStoreWindow *appStoreWindow = (INAppStoreWindow *)[self window];

// self.titleView is a an IBOutlet to an NSView that has been configured in IB with everything you want in the title bar
self.windowTitleBarView.frame = appStoreWindow.titleBarView.bounds;
self.windowTitleBarView.autoresizingMask = NSViewWidthSizable | NSViewHeightSizable;
[appStoreWindow.titleBarView addSubview:self.windowTitleBarView];

这两个棘手的部分是使这个拆分视图的行为类似于标题栏(即仍然允许您拖动窗口),并将标题栏中的拆分视图与窗口中的主拆分视图同步,因此对用户来说它们看起来是相同的。
要解决第一个问题,您需要做的不仅仅是在标题栏nssplitview子类中从-mouseDownCanMovewWindow返回yes。若您这样做,则平铺栏的任何子视图都不会响应鼠标事件。相反,请执行以下操作:
@implementation MyTitleBarSplitView

- (BOOL)mouseDownCanMoveWindow
{
    return NO;
}

// Code below adapted from http://www.cocoabuilder.com/archive/cocoa/219261-conditional-mousedowncanmovewindow-for-nsview.html
- (void)mouseDown:(NSEvent*)theEvent
{
    NSWindow *window = [self window];
    NSPoint mouseLocation = [theEvent locationInWindow];
    NSRect dividerRect = NSMakeRect(NSMaxX([[[self subviews] objectAtIndex:0] frame]),
                                    NSMinY([self bounds]),
                                    [self dividerThickness],
                                    NSHeight([self bounds]));
    dividerRect = NSInsetRect(dividerRect, -2, 0);
    NSPoint mouseLocationInMyCoords = [self convertPoint:mouseLocation fromView:nil];
    if (![self mouse:mouseLocationInMyCoords inRect:dividerRect])
    {
        mouseLocation = [window convertBaseToScreen:mouseLocation];
        NSPoint origin = [window frame].origin;
        // Now we loop handling mouse events until we get a mouse up event.
        while ((theEvent = [NSApp nextEventMatchingMask:NSLeftMouseDownMask|NSLeftMouseDraggedMask|NSLeftMouseUpMask untilDate:[NSDate distantFuture] inMode:NSEventTrackingRunLoopMode dequeue:YES])&&([theEvent type]!=NSLeftMouseUp))
        {
            @autoreleasepool
            {
                NSPoint currentLocation = [window convertBaseToScreen:[theEvent locationInWindow]];
                origin.x += currentLocation.x-mouseLocation.x;
                origin.y += currentLocation.y-mouseLocation.y;
                // Move the window by the mouse displacement since the last event.
                [window setFrameOrigin:origin];
                mouseLocation = currentLocation;
            }
        }
        [self mouseUp:theEvent];
        return;
    }

    [super mouseDown:theEvent];
}

@end

业务的第二个顺序是同步这两个分离视图。使您的控制器类(可能是窗口控制器,但代码中有意义的部分)成为主内容拆分视图和标题栏拆分视图的委托。然后,实现以下两种nssplitview委托方法:
@implementation MyController
{
    BOOL updatingLinkedSplitview;
}

- (CGFloat)splitView:(NSSplitView *)splitView constrainSplitPosition:(CGFloat)proposedPosition ofSubviewAt:(NSInteger)dividerIndex
{
    // If already updating a split view, return early to avoid infinite loop and stack overflow
    if (updatingLinkedSplitview) return proposedPosition;

    if (splitView == self.mainSplitView)
    {
        // Main splitview is being resized, so manually resize the title bar split view
        updatingLinkedSplitview = YES;
        [self.titleBarSplitView setPosition:proposedPosition ofDividerAtIndex:dividerIndex];
        updatingLinkedSplitview = NO;
    }
    else if (splitView == self.titleBarSplitView)
    {
        // Title bar splitview is being resized, so manually resize the main split view
        updatingLinkedSplitview = YES;
        [self.mainSplitView setPosition:proposedPosition ofDividerAtIndex:dividerIndex];
        updatingLinkedSplitview = NO;
    }

    return proposedPosition;
}

- (void)splitView:(NSSplitView *)splitView resizeSubviewsWithOldSize:(NSSize)oldSize
{
    // This is to synchronize the splitter positions when the window is first loaded
    if (splitView == self.titleBarSplitView)
    {
        NSRect leftFrame = NSMakeRect(NSMinX([self.leftTitleBarView frame]),
                                      NSMinY([self.leftTitleBarView frame]),
                                      NSWidth([self.leftMainSplitView frame]),
                                      NSHeight([self.leftTitleBarView frame]));
        NSRect rightFrame = NSMakeRect(NSMaxX(leftFrame) + [splitView dividerThickness],
                                      NSMinY([self.rightTitleBarView frame]),
                                      NSWidth([self.rightMainSplitView frame]),
                                      NSHeight([self.rightTitleBarView frame]));

        [self.leftTitleBarView setFrame:leftFrame];
        [self.rightTitleBarView setFrame:rightFrame];
    }
}

关于objective-c - INAppStoreWindow标题栏上的NSSplitView分隔线,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/15092738/

10-12 01:33