我是macos的新手。我正在为macOS开发一个应用,其中希望用户拥有侧面板,并且他可以根据需要隐藏/取消隐藏。
我已使用split view
将屏幕分为3部分。左右面板与视图(非拆分视图)的宽度成比例。
我想要的是当用户隐藏左面板时,左窗格应该隐藏自身,并且窗口的左锚点应移到右侧,而右锚点应保持在其位置。
类似地,如果用户隐藏右面板,则右面板也应该被隐藏。窗口的右锚点应向左移动,左锚点应保持在其位置。
当用户想要隐藏/取消隐藏左面板时,将调用以下函数。
guard let window = self.view.window else {
return
}
var frame = window.frame
if window.isZoomed {
if leftPane.contentView!.isHidden {
self.leftPane.isHidden = false
self.leftPane.contentView?.isHidden = false
} else {
self.leftPane.contentView?.isHidden = true
}
} else {
if leftPaneHidden {
self.leftPane.isHidden = false
frame = NSRect(x: (frame.origin.x - leftPane.frame.size.width), y: frame.origin.y, width: (frame.size.width + leftPane.frame.size.width), height: frame.size.height)
leftPaneHidden = false
} else {
self.leftPane.isHidden = true
frame = NSRect(x: (frame.origin.x + leftPane.frame.size.width), y: frame.origin.y, width: (frame.size.width - leftPane.frame.size.width), height: frame.size.height)
leftPaneHidden = true
}
self.view.window?.setFrame(frame, display: true, animate: true)
我希望隐藏左侧面板,因为它可在知名度应用程序中使用。
答案 0 :(得分:0)
如果您确实想让窗口缩小/扩展,那么这里是一个有效的演示。如果您希望它按照Xcode的方式工作,请删除与框架相关的内容。它是通过程序实现的,因此我不必描述情节提要,但是您可以轻松地使用情节提要执行相同的操作。左右视图中唯一的代码设置背景色,以便您可以看到它的工作原理。
SplitViewController:
class MainSplitViewController: NSSplitViewController {
weak var leftItem: NSSplitViewItem?
weak var rightItem: NSSplitViewItem?
convenience init(identifier: NSUserInterfaceItemIdentifier) {
self.init()
splitView.identifier = identifier
splitView.wantsLayer = true
splitView.layer?.backgroundColor = NSColor.darkGray.cgColor
splitView.dividerStyle = .thin
let vcL = SubViewController(NSView(), backgroundColor: .red)
let vcM = MainViewController(MainView(self), backgroundColor: .green)
let vcR = SubViewController(NSView(), backgroundColor: .blue)
vcL.view.widthAnchor.constraint(greaterThanOrEqualToConstant: 100).isActive = true
vcM.view.widthAnchor.constraint(greaterThanOrEqualToConstant: 100).isActive = true
vcR.view.widthAnchor.constraint(greaterThanOrEqualToConstant: 100).isActive = true
let sidebarItem = NSSplitViewItem(viewController: vcL)
sidebarItem.canCollapse = true
sidebarItem.holdingPriority = NSLayoutConstraint.Priority(NSLayoutConstraint.Priority.defaultLow.rawValue + 1)
addSplitViewItem(sidebarItem)
leftItem = sidebarItem
let mainItem = NSSplitViewItem(viewController: vcM)
addSplitViewItem(mainItem)
let inspItem = NSSplitViewItem(viewController: vcR)
inspItem.canCollapse = true
inspItem.holdingPriority = NSLayoutConstraint.Priority(NSLayoutConstraint.Priority.defaultLow.rawValue + 1)
addSplitViewItem(inspItem)
rightItem = inspItem
}
}
中间视图,带有用于切换侧视图的按钮:
class MainView: NSView, DebugHelper {
weak var splitViewController: MainSplitViewController?
func labeledButton(_ stringValue: String = "") -> NSButton {
let button = NSButton()
button.translatesAutoresizingMaskIntoConstraints = false
button.heightAnchor.constraint(equalToConstant: 28.0)
button.bezelStyle = NSButton.BezelStyle.rounded
button.title = stringValue
return button
}
private func adjustWindowFrame(_ item: NSSplitViewItem, left: Bool) {
let view = item.viewController.view
let frame = window!.frame
if item.isCollapsed {
let minX = left ? frame.minX - view.frame.width : frame.minX
let newFrame = NSRect(x: minX, y: frame.minY, width: frame.width + view.frame.width, height: frame.height)
window!.setFrame(newFrame, display: true)
} else {
let minX = left ? frame.minX + view.frame.width : frame.minX
let newFrame = NSRect(x: minX, y: frame.minY, width: frame.width - view.frame.width, height: frame.height)
window!.setFrame(newFrame, display: true)
}
item.isCollapsed = !item.isCollapsed
}
@objc func collapseLeft(_ sender: Any) {
guard let item = splitViewController?.leftItem else { return }
adjustWindowFrame(item, left: true)
}
@objc func collapseRight(_ sender: Any) {
guard let item = splitViewController?.rightItem else { return }
adjustWindowFrame(item, left: false)
}
convenience init(_ parent: MainSplitViewController) {
self.init(frame: .zero)
splitViewController = parent
translatesAutoresizingMaskIntoConstraints = false
let lButton = labeledButton("Collapse Left")
addSubview(lButton)
lButton.bottomAnchor.constraint(equalTo: bottomAnchor, constant: -20).isActive = true
lButton.centerXAnchor.constraint(equalTo: centerXAnchor, constant: -100).isActive = true
lButton.action = #selector(collapseLeft)
lButton.target = self
let rButton = labeledButton("Collapse Right")
addSubview(rButton)
rButton.bottomAnchor.constraint(equalTo: bottomAnchor, constant: -20).isActive = true
rButton.centerXAnchor.constraint(equalTo: centerXAnchor, constant: 100).isActive = true
rButton.action = #selector(collapseRight)
rButton.target = self
}
}
class MainViewController: NSViewController {
convenience init(_ view: NSView, backgroundColor: NSColor = .white) {
self.init()
self.view = view
self.view.wantsLayer = true
self.view.layer?.backgroundColor = backgroundColor.cgColor
}
}