[英]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:
我尝试了以下方法:
constrainMaxCoordinate
and constrainMinCoordinate
didn't work.constrainMaxCoordinate
和constrainMinCoordinate
不起作用。 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.