为什么我的拆分视图表现得像这样,我该如何解决?

时间:2021-08-09 17:44:33

I created a split view controller that displays two views, like this : 为什么我的拆分视图表现得像这样,我该如何解决?

我创建了一个分割视图控制器,显示两个视图,如下所示:

When I compile it, it gives me this result : 为什么我的拆分视图表现得像这样,我该如何解决?

当我编译它时,它给了我这个结果:

Unfortunately, the first view isn't visible and I must drag from the left hand side of the window to see the two views :
为什么我的拆分视图表现得像这样,我该如何解决?

不幸的是,第一个视图不可见,我必须从窗口的左侧拖动才能看到两个视图:

First, why is the split view behaving like this ? Why isn't it already at the right size from the beginning ?
If I add this line to the viewDidLoad() function of my SplitViewController :

首先,为什么拆分视图表现得像这样?为什么它从一开始就没有合适的尺寸?如果我将此行添加到我的SplitViewController的viewDidLoad()函数:

splitView.adjustSubviews()

Then the two view appears, with equal size, but I don't understand what the adjustSubviews() function does exactly, and I can't control the position of either.

然后两个视图出现,大小相同,但我不明白adjustSubviews()函数究竟做了什么,我无法控制其中任何一个的位置。

How to fix it programmatically ? How to adjust the size of each view ? Is there a way to adjust it in interface builder ?

如何以编程方式修复它?如何调整每个视图的大小?有没有办法在界面构建器中调整它?

Thank you.

EDIT : There is now a bounty of 50 points for this question

编辑:这个问题现在有50分的赏金

3 个解决方案

#1


7  

Since NSSplitViewController is auto-layout based, you can use auto-layout either if you want a fixed or a dynamic behavior on your views.

由于NSSplitViewController是基于自动布局的,因此如果您希望视图具有固定或动态行为,则可以使用自动布局。

For a fixed width what you can do is add a custom view inside your 2 views with a 0 constraint to all the edges and a fixed width. For example:

对于固定宽度,您可以在2个视图中添加自定义视图,对所有边和固定宽度使用0约束。例如:

为什么我的拆分视图表现得像这样,我该如何解决?

Then the window will get expanded as follows:

然后窗口将扩展如下:

为什么我的拆分视图表现得像这样,我该如何解决?

#2


1  

You should be able to set the position of the divider in the split view using (assuming 2 views in the split view):

您应该能够使用(在分割视图中假设2个视图)在拆分视图中设置分隔符的位置:

self.splitView.setPosition(200.0, ofDividerAtIndex: 0)

self.splitView.setPosition(200.0,ofDividerAtIndex:0)

That would set the first divider at position 200.0

这会将第一个分隔线设置在200.0位置

#3


0  

We need to set sizes of NSSplitView's sub-views directly. Setting ones through splitViewItems seems to have no effect.

我们需要直接设置NSSplitView的子视图的大小。通过splitViewItems设置似乎没有任何效果。

// NSSplitViewController
- (void)viewDidLoad {
    [super viewDidLoad];

    NSView *view1 = self.splitView.subviews[0];
    NSRect rect = view1.frame;
    rect.size.width = 150;
    view1.frame = rect;

    NSView *view2 = self.splitView.subviews[1];
    rect = view2.frame;
    rect.size.width = 300;
    view2.frame = rect;
}

#1


7  

Since NSSplitViewController is auto-layout based, you can use auto-layout either if you want a fixed or a dynamic behavior on your views.

由于NSSplitViewController是基于自动布局的,因此如果您希望视图具有固定或动态行为,则可以使用自动布局。

For a fixed width what you can do is add a custom view inside your 2 views with a 0 constraint to all the edges and a fixed width. For example:

对于固定宽度,您可以在2个视图中添加自定义视图,对所有边和固定宽度使用0约束。例如:

为什么我的拆分视图表现得像这样,我该如何解决?

Then the window will get expanded as follows:

然后窗口将扩展如下:

为什么我的拆分视图表现得像这样,我该如何解决?

#2


1  

You should be able to set the position of the divider in the split view using (assuming 2 views in the split view):

您应该能够使用(在分割视图中假设2个视图)在拆分视图中设置分隔符的位置:

self.splitView.setPosition(200.0, ofDividerAtIndex: 0)

self.splitView.setPosition(200.0,ofDividerAtIndex:0)

That would set the first divider at position 200.0

这会将第一个分隔线设置在200.0位置

#3


0  

We need to set sizes of NSSplitView's sub-views directly. Setting ones through splitViewItems seems to have no effect.

我们需要直接设置NSSplitView的子视图的大小。通过splitViewItems设置似乎没有任何效果。

// NSSplitViewController
- (void)viewDidLoad {
    [super viewDidLoad];

    NSView *view1 = self.splitView.subviews[0];
    NSRect rect = view1.frame;
    rect.size.width = 150;
    view1.frame = rect;

    NSView *view2 = self.splitView.subviews[1];
    rect = view2.frame;
    rect.size.width = 300;
    view2.frame = rect;
}