如何约束NSSplitView?

时间:2012-02-25 07:12:24

标签: objective-c macos cocoa nssplitview

您好我正在尝试约束NSSplitView的最大和最小坐标。我创建了一个视图控制器并将其指定为NSSplitView的委托。然而,调用委托方法,拆分视图不会限制到我尝试将其设置为的位置。关于我做错了什么的建议?

- (CGFloat)splitView:(NSSplitView *)splitView constrainMinCoordinate:(CGFloat)proposedMinimumPosition ofSubviewAt:(NSInteger)dividerIndex 
{
    NSLog(@"Constrain min");

    if (proposedMinimumPosition < 75) 
    {
        proposedMinimumPosition = 75;
    }

    return proposedMinimumPosition;
}

- (CGFloat)splitView:(NSSplitView *)splitView constrainMaxCoordinate:(CGFloat)proposedMax ofSubviewAt:(NSInteger)dividerIndex
{
    NSLog(@"Constrain max");

    if (proposedMax > 200) 
    {
        proposedMax = 200;
    }

    return proposedMax ;
}

2 个答案:

答案 0 :(得分:1)

Let's assume you want each one of two sections on a vertical splitter to be at least 70.0 high, then what you'd do this:

- (CGFloat)     splitView:(NSSplitView *)splitView
   constrainMinCoordinate:(CGFloat)proposedMin
              ofSubviewAt:(NSInteger)dividerIndex
{
    return 70.0;
}

- (CGFloat)     splitView:(NSSplitView *)splitView
   constrainMaxCoordinate:(CGFloat)proposedMin
              ofSubviewAt:(NSInteger)dividerIndex
{
    return splitView.frame.size.height - 70.0;
}

The reason for the subtraction is to dynamically account for any resizing (with autolayout, for example) of the overall NSplitView instance. If you're working with a horizontal one, then you'd need to calculate against .width instead of .height. If you have more than 2 subviews, the idea can be extended by looking at dividerIndex and applying values as you see fit.

答案 1 :(得分:0)

我通过这样做解决了这个问题。

- (BOOL)splitView:(NSSplitView *)splitView canCollapseSubview:(NSView *)subview 
{
    return NO;
}