xcode 设置 NSSplitViews 的最小宽度
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/28001996/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Setting minimum width of NSSplitViews
提问by Mike Pulsifer
I'm having a heck of a time setting up a simple split view. The first split view is collapsed. I need to set a minimum width for it. Everything I see online (scarce for NSSplitViewController/NSSplitView) is for Objective-C, puts everything in the app delegate, and uses XIBs.
我有很多时间设置一个简单的拆分视图。第一个拆分视图已折叠。我需要为它设置一个最小宽度。我在网上看到的所有内容(NSSplitViewController/NSSplitView 很少见)都是针对 Objective-C 的,将所有内容放在应用程序委托中,并使用 XIB。
Here's the scenario: Window Controller with a segue to a SplitView Controller, which has two split views (2 view controllers).
这是场景:窗口控制器与 SplitView 控制器的 segue,它有两个拆分视图(2 个视图控制器)。
Which object needs to have the NSSplitViewDelegate?
哪个对象需要有 NSSplitViewDelegate?
EDIT: Adding code snippet:For example, I have this:
编辑:添加代码片段:例如,我有这个:
import Cocoa
class ViewController: NSSplitViewController, NSSplitViewDelegate {
@IBOutlet weak var pdlSplitView: NSSplitView!
override func viewDidLoad() {
super.viewDidLoad()
}
override func splitView(splitView: NSSplitView, constrainMinCoordinate proposedMinimumPosition: CGFloat, ofSubviewAt dividerIndex: Int) -> CGFloat {
return proposedMinimumPosition + 200
}
}
Is there more that I'm missing?
还有更多我想念的吗?
Thanks
谢谢
UPDATE
更新
Based on comments below, I've made a change, but now I get a sigAbort on the class definition for the AppDelegate. Full code
根据下面的评论,我进行了更改,但现在我在 AppDelegate 的类定义上获得了 sigAbort。完整代码
ViewController:
视图控制器:
import Cocoa
class ViewController: NSSplitViewController, NSSplitViewDelegate {
@IBOutlet weak var pdlSplitView: NSSplitView!
let publicDataListings : PDL = PDL()
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view.
self.pdlSplitView.delegate = self
}
override func splitView(splitView: NSSplitView, constrainMinCoordinate proposedMinimumPosition: CGFloat, ofSubviewAt dividerIndex: Int) -> CGFloat {
return proposedMinimumPosition + 200
}
}
SidebarViewController:
侧边栏视图控制器:
import Cocoa
class SidebarViewController: NSViewController {
override func viewDidLoad() {
super.viewDidLoad()
// Do view setup here.
}
}
DatasetViewController:
数据集视图控制器:
import Cocoa
class DatasetViewController: NSViewController, NSSplitViewDelegate {
override func viewDidLoad() {
super.viewDidLoad()
// Do view setup here.
}
}
UpdateI took away my custom NSSplitViewController class and created two NSSplitView classes, one with the constraint method. Now, I see both subviews, but they're far smaller than they should be:
更新我拿走了我的自定义 NSSplitViewController 类并创建了两个 NSSplitView 类,一个带有约束方法。现在,我看到了两个子视图,但它们比应有的要小得多:
Is there anyone at allthat has done this with Swift and Storyboards?
有没有人用 Swift 和 Storyboards 做到这一点?
回答by Mythlandia
No coding is required to set a minimum width in a storyboard with auto layout for a NSSplitViewController/NSSplitView.
无需编码即可在具有自动布局的故事板中为 NSSplitViewController/NSSplitView 设置最小宽度。
Select the CustomView that you require a minimum width for (e.g. 200), and add a width constraint set to the required value which will add a "Equal" constraint (e.g. Custom View.Width equals 200).
选择您需要最小宽度的 CustomView(例如 200),并添加设置为所需值的宽度约束,这将添加“Equal”约束(例如 Custom View.Width 等于 200)。
Next locate that new constraint and change the constraint relation to "Greater Than or Equal" (e.g. so you now have width ≥ 200).
接下来找到该新约束并将约束关系更改为“大于或等于”(例如,您现在的宽度≥ 200)。
You now have a minimum width in an NSSplitView. You can then use the Priority field to resolve any conflicts with any other auto layout constraints.
您现在在 NSSplitView 中有一个最小宽度。然后您可以使用 Priority 字段来解决与任何其他自动布局约束的任何冲突。
回答by Sinisa Drpa
- Set NSSplitViewController as delegate of NSSplitView (the split view you want to constrain). In your case it should be - in xib hook the delegate outlet of the NSSplitView to file owner (I guess the file owner is NSSplitViewController subclass)
Implement
- (CGFloat)splitView:(NSSplitView *)splitView constrainMinCoordinate:(CGFloat)proposedMin ofSubviewAt:(NSInteger)dividerIndex { ... }
- 将 NSSplitViewController 设置为 NSSplitView 的委托(您要约束的拆分视图)。在您的情况下,它应该是 - 在 xib 中将 NSSplitView 的委托出口挂钩到文件所有者(我猜文件所有者是 NSSplitViewController 子类)
实施
- (CGFloat)splitView:(NSSplitView *)splitView constrainMinCoordinate:(CGFloat)proposedMin ofSubviewAt:(NSInteger)dividerIndex { ... }
in NSSplitViewController
在 NSSplitViewController 中