2

我很难让 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;

}

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

如果我单击拆分器,它会按原样恢复到 200 像素。我怎样才能阻止它移动?

我已经尝试过 Autolayout,使用起来有点像噩梦,所以我真的禁用了它并手动尝试这样做,没有任何乐趣..

有任何想法吗?

4

3 回答 3

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))
    }
}
于 2015-05-09T21:22:33.213 回答
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];
}
于 2013-07-10T13:05:52.520 回答
0
- (BOOL)splitView:(NSSplitView *)aSplitView shouldAdjustSizeOfSubview:(NSView *)subview
{
    return subview == rightView;
}

将此添加到您的委托。

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

于 2017-08-21T21:34:28.493 回答