gpt4 book ai didi

macos - NSSplitviewController 以编程方式

转载 作者:行者123 更新时间:2023-12-03 16:17:01 24 4
gpt4 key购买 nike

我有一个 NSSplitViewController,其中有 2 个 View Controller 作为 splitViewItems。这工作得很好。

现在我想在 Storyboard中为我的 Splitviewcontroller 设置一个自己的 SplitViewController 类。我给它我自己的类启动应用程序。但现在我只看到一个空窗口。

如果我设置自己的 splitviewcontroller 类,我现在是否可以通过编程方式设置 splitviewcontroller?

如果是,我必须使用哪些代码才能再次在 splitview Controller 中显示两个 View Controller ?

更新

import Cocoa

class SplitViewController: NSSplitViewController {
override func viewDidLoad() {
print("Test")
}
}

enter image description here

最佳答案

这里是 Xcode 9 Playground (Swift 4),展示了如何从代码设置 NSSplitViewController

import Cocoa
import PlaygroundSupport

class ViewController: NSViewController {

private let backgroundColor: NSColor

init(backgroundColor: NSColor) {
self.backgroundColor = backgroundColor
super.init(nibName: nil, bundle: nil)
}

required init?(coder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}

override func loadView() {
view = NSView()
view.wantsLayer = true
view.layer?.backgroundColor = backgroundColor.cgColor
}
}


class MainSplitViewController: NSSplitViewController {

private let splitViewResorationIdentifier = "com.company.restorationId:mainSplitViewController"

lazy var vcA = ViewController(backgroundColor: .red)
lazy var vcB = ViewController(backgroundColor: .green)
lazy var vcC = ViewController(backgroundColor: .blue)

override init(nibName nibNameOrNil: NSNib.Name?, bundle nibBundleOrNil: Bundle?) {
super.init(nibName: nibNameOrNil, bundle: nibBundleOrNil)
setupUI()
setupLayout()
}

required init?(coder: NSCoder) {
super.init(coder: coder)
}

}


extension MainSplitViewController {

private func setupUI() {

view.wantsLayer = true

splitView.dividerStyle = .paneSplitter
splitView.autosaveName = NSSplitView.AutosaveName(rawValue: splitViewResorationIdentifier)
splitView.identifier = NSUserInterfaceItemIdentifier(rawValue: splitViewResorationIdentifier)
}

private func setupLayout() {

minimumThicknessForInlineSidebars = 180

let itemA = NSSplitViewItem(sidebarWithViewController: vcA)
itemA.minimumThickness = 80
addSplitViewItem(itemA)

let itemB = NSSplitViewItem(contentListWithViewController: vcB)
itemB.minimumThickness = 100
addSplitViewItem(itemB)

let itemC = NSSplitViewItem(viewController: vcC)
itemC.minimumThickness = 80
addSplitViewItem(itemC)
}

}

let vc = MainSplitViewController()
vc.view.frame = CGRect(x: 0, y: 0, width: 400, height: 300)

PlaygroundPage.current.liveView = vc

更新:

与上述版本类似的版本,但针对 macOS 10.10。

import Cocoa
import PlaygroundSupport

class ViewController: NSViewController {

private let backgroundColor: NSColor

init(backgroundColor: NSColor) {
self.backgroundColor = backgroundColor
super.init(nibName: nil, bundle: nil)
}

required init?(coder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}

override func loadView() {
view = NSView()
view.wantsLayer = true
view.layer?.backgroundColor = backgroundColor.cgColor
}
}


class MainSplitViewController: NSSplitViewController {

private let splitViewResorationIdentifier = "com.company.restorationId:mainSplitViewController"

lazy var vcA = ViewController(backgroundColor: .red)
lazy var vcB = ViewController(backgroundColor: .green)
lazy var vcC = ViewController(backgroundColor: .blue)

override init(nibName nibNameOrNil: NSNib.Name?, bundle nibBundleOrNil: Bundle?) {
super.init(nibName: nibNameOrNil, bundle: nibBundleOrNil)
setupUI()
setupLayout()
}

required init?(coder: NSCoder) {
super.init(coder: coder)
}

}


extension MainSplitViewController {

private func setupUI() {

view.wantsLayer = true

splitView.dividerStyle = .paneSplitter
splitView.autosaveName = NSSplitView.AutosaveName(rawValue: splitViewResorationIdentifier)
splitView.identifier = NSUserInterfaceItemIdentifier(rawValue: splitViewResorationIdentifier)

vcA.view.widthAnchor.constraint(greaterThanOrEqualToConstant: 80).isActive = true
vcB.view.widthAnchor.constraint(greaterThanOrEqualToConstant: 100).isActive = true
vcC.view.widthAnchor.constraint(greaterThanOrEqualToConstant: 80).isActive = true
}

private func setupLayout() {

let sidebarItem = NSSplitViewItem(viewController: vcA)
sidebarItem.canCollapse = true
sidebarItem.holdingPriority = NSLayoutConstraint.Priority(NSLayoutConstraint.Priority.defaultLow.rawValue + 1)
addSplitViewItem(sidebarItem)

let xibItem = NSSplitViewItem(viewController: vcB)
addSplitViewItem(xibItem)

let codeItem = NSSplitViewItem(viewController: vcC)
addSplitViewItem(codeItem)
}

}

let vc = MainSplitViewController()
vc.view.frame = CGRect(x: 0, y: 0, width: 400, height: 300)

PlaygroundPage.current.liveView = vc

关于macos - NSSplitviewController 以编程方式,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44309088/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com