所有直接复用控制器的方法都是异常的强大,都是必须掌握的技能。
🌰🌰:
//弹出NSViewController
func showSheetController() {
let controller = NNBatchClassCreateController()
let rect = CGRectMake(0, 0, kScreenWidth*0.25, kScreenHeight*0.25)
NSWindow.show(with: controller, size: rect.size) { (response) in
DDLog(response)
}
}
//NSViewController 中关闭弹窗:
NSWindow.end(with: self, response: NSApplication.ModalResponse.OK)
源码:
@objc public extension NSWindow {
/// 默认大小
static var defaultRect: CGRect {
return CGRectMake(0, 0, kScreenWidth*0.4, kScreenHeight*0.5)
}
static func create(_ rect: CGRect = NSWindow.defaultRect, title: String = NSApplication.appName) -> Self {
// let style = NSWindow.StyleMask.titled.rawValue | NSWindow.StyleMask.closable.rawValue | NSWindow.StyleMask.miniaturizable.rawValue | NSWindow.StyleMask.resizable.rawValue
let style: NSWindow.StyleMask = [.titled, .closable, .miniaturizable, .resizable]
let window = self.init(contentRect: rect, styleMask: style, backing: .buffered, defer: false)
window.title = title
window.titlebarAppearsTransparent = true
return window;
}
static func create(_ rect: CGRect = NSWindow.defaultRect, controller: NSViewController) -> Self {
let window = Self.create(rect, title: controller.title ?? "")
window.contentViewController = controller;
return window;
}
static func createMain(_ rect: CGRect = NSWindow.defaultRect, title: String = NSApplication.appName) -> Self {
let window = Self.create(rect, title: title)
window.contentMinSize = window.frame.size;
window.makeKeyAndOrderFront(self)
window.center()
return window;
}
/// 下拉弹窗
static func show(with controller: NSViewController, size: CGSize, handler: ((NSApplication.ModalResponse) -> Void)? = nil) {
controller.preferredContentSize = size
let rect = CGRectMake(0, 0, size.width, size.height)
let panel = Self.create(rect, controller: controller)
NSApp.mainWindow?.beginSheet(panel, completionHandler: handler)
}
/// 下拉弹窗关闭
static func end(with controller: NSViewController, response: NSApplication.ModalResponse) {
guard let window = controller.view.window else { return }
if window.isMember(of: Self.classForCoder()) == true {
// view.window?.orderOut(self)
NSApp.mainWindow?.endSheet(window, returnCode: response)
}
}
}