简体   繁体   English

使用 Swift 设置 NSSplitViewController 窗格的宽度

[英]Set NSSplitViewController Pane's Width Using Swift

Xcode 8.2.1, Swift 3.0.2, macOS 10.12 Xcode 8.2.1,Swift 3.0.2,macOS 10.12

I have an NSSplitViewController that has 3 panes (please note the NS ; this is for a Mac app).我有一个NSSplitViewController有 3 个窗格(请注意NS ;这是用于Mac应用程序)。

I'm trying to set the left-most pane to always have a fixed width and not be resizable.我正在尝试将最左侧的窗格设置为始终具有固定宽度并且不可调整大小。 I have tried the following:我尝试了以下方法:

  • For some reason, auto layout constraints are not available in the Storyboard editor on the child view of the pane.由于某种原因,自动布局约束在窗格子视图的 Storyboard 编辑器中不可用。
  • Setting constrainMaxCoordinate and constrainMinCoordinate didn't work.设置constrainMaxCoordinateconstrainMinCoordinate不起作用。

Has anyone else gotten this to work lately?最近有没有人让这个工作? There are lots of articles about this topic, but their toolsets are several years old and none of their suggestions have worked for me.有很多关于这个主题的文章,但是他们的工具集已经有几年的历史了,他们的建议都没有对我有用。 Thanks!谢谢!

在此处输入图像描述

I finally figured this out.我终于想通了这一点。 I had to set an auto layout constraint programmatically.我必须以编程方式设置自动布局约束。 Setting a constraint on an NSSplitViewItem was not available in the storyboard editor (Xcode 8.1).在故事板编辑器 (Xcode 8.1) 中无法NSSplitViewItem设置约束。

Here's how I did it:这是我如何做到的:

//This is my SplitViewController subclass
class MainSplitVC: NSSplitViewController {

  //Outlet to the menu pane
  @IBOutlet weak var menuPane: NSSplitViewItem!

  override func viewDidLoad() {
    super.viewDidLoad()

    //Hard-code the width of the menu pane
    NSLayoutConstraint(item: menuPane.viewController.view, attribute: NSLayoutAttribute.width, relatedBy: NSLayoutRelation.equal, toItem: nil, attribute: NSLayoutAttribute.notAnAttribute, multiplier: 1, constant: 80).isActive = true
  }
}

I hope that helps someone else.我希望能帮助别人。

Usually you should set a layout constraints to subviews of the child view.通常你应该为子视图的子视图设置布局约束。

For example, if you have an NSOutlineView in the left pane, just set it's width constraint to a fixed value.例如,如果左窗格中有一个 NSOutlineView,只需将其宽度约束设置为固定值。

The size of a pane's view is calculated by the constraints of it's subviews.窗格视图的大小由其子视图的约束计算。

在此处输入图片说明 在此处输入图片说明

In case you don't use layout constraints for setting the width of a NSSplitViewItem , you can use code like this:如果您不使用布局约束来设置NSSplitViewItem的宽度,则可以使用如下代码:

splitViewController.splitView.setPosition(600, ofDividerAt: 0)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM