窗口调整大小的NSSplitView固定拆分器?

时间:2013-07-03 07:15:10

标签: objective-c xcode cocoa

让NSSplitView表现自己有点困难。

目前我所拥有的是:

NSWindow
   NSView
      NSSplitView
         navView <NSView>
         contentView <NSView>

当我调整窗口大小时,我遇到的问题是分离器移位位置。

在拆分视图委托中,我已经得到了:

-(CGFloat)splitView:(NSSplitView *)splitView constrainMaxCoordinate:(CGFloat)proposedMaximumPosition ofSubviewAt:(NSInteger)dividerIndex{
    return 200;
}

-(CGFloat)splitView:(NSSplitView *)splitView constrainMinCoordinate:(CGFloat)proposedMinimumPosition ofSubviewAt:(NSInteger)dividerIndex{
    return 200;

}

尽管如此,当我调整窗口大小时,分割器仍然移动。

如果我点击它的分离器,它会按原样快速恢复到200px。我怎么能阻止它移动?

我尝试过使用Autolayout,这是一个噩梦,所以我确实禁用了它,并且手动尝试着它,没有任何乐趣..

有什么想法吗?

3 个答案:

答案 0 :(得分:3)

我为Swift编写了上面的代码并扩展了它,可以定义是否必须首选左视图或右视图:

var preferringLeftSideOfSplitView = true

func splitView(splitView: NSSplitView, resizeSubviewsWithOldSize oldSize: NSSize) {
    var dividerThickness = splitView.dividerThickness

    var leftRect = splitView.subviews[0].frame
    var rightRect = splitView.subviews[1].frame

    // Resizing and placing the left view
    splitView.subviews[0].setFrameOrigin(NSMakePoint(0, 0))
    if self.preferringLeftSideOfSplitView == true {
        splitView.subviews[0].setFrameSize(NSMakeSize(leftRect.width, splitView.frame.size.height))
    } else {
        splitView.subviews[0].setFrameSize(NSMakeSize(splitView.frame.size.width - rightRect.width - dividerThickness, splitView.frame.size.height))
    }

    // Resizing and placing the right view
    if self.preferringLeftSideOfSplitView == true {
        splitView.subviews[1].setFrameOrigin(NSMakePoint(leftRect.size.width + dividerThickness, 0))
        splitView.subviews[1].setFrameSize(NSMakeSize(splitView.frame.size.width - leftRect.size.width - dividerThickness, splitView.frame.size.height))
    } else {
        splitView.subviews[1].setFrameOrigin(NSMakePoint(splitView.frame.size.width - rightRect.width, 0))
        splitView.subviews[1].setFrameSize(NSMakeSize(rightRect.size.width, splitView.frame.size.height))
    }
}

答案 1 :(得分:2)

我已经解决了......

-(void)splitView:(NSSplitView *)sender resizeSubviewsWithOldSize:(NSSize)oldSize
{
    CGFloat dividerThickness = [sender dividerThickness];
    NSRect leftRect  = [[[sender subviews] objectAtIndex:0] frame];
    NSRect rightRect = [[[sender subviews] objectAtIndex:1] frame];
    NSRect newFrame  = [sender frame];

    leftRect.size.height = newFrame.size.height;
    leftRect.origin = NSMakePoint(0, 0);
    rightRect.size.width = newFrame.size.width - leftRect.size.width
- dividerThickness;
    rightRect.size.height = newFrame.size.height;
    rightRect.origin.x = leftRect.size.width + dividerThickness;

    [[[sender subviews] objectAtIndex:0] setFrame:leftRect];
    [[[sender subviews] objectAtIndex:1] setFrame:rightRect];
}

答案 2 :(得分:0)

- (BOOL)splitView:(NSSplitView *)aSplitView shouldAdjustSizeOfSubview:(NSView *)subview
{
    return subview == rightView;
}

将此添加到您的代理人。

如果您的NSSplitView包含lefView和rightView,这会使leftView保持固定宽度,并且在调整主窗口大小时,rightView将调整大小。