OSX应用:如何使window最大化?
OSX Application: how to make window maximized?
我正在开发一个 mac 应用程序,我喜欢使初始 window 处于最大化状态,就像您按下带有加号的绿色按钮时一样。
我不想全屏。
通过使用 NSScreen 的 visibleFrame
作为目标框架,您可以 "zoom" 一个 window 到最大可用 space。假设 window
是您的 NSWindow IBOutlet:
if let screen = NSScreen.mainScreen() {
window.setFrame(screen.visibleFrame, display: true, animate: true)
}
例如,在 AppDelegate.swift:
import Cocoa
@NSApplicationMain
class AppDelegate: NSObject, NSApplicationDelegate {
@IBOutlet weak var window: NSWindow!
func applicationDidFinishLaunching(aNotification: NSNotification) {
if let screen = NSScreen.mainScreen() {
window.setFrame(screen.visibleFrame, display: true, animate: true)
}
}
处于缩放状态的应用程序与 "maximized." 不同,但它几乎可以是任何东西。例如,尝试缩放 Safari window。
假设您真的想要 "maximized" 而不是 "zoom",那么 Eric 就在正确的轨道上,但可以做得更好。首先,你应该使用 window 的屏幕(如果有的话)。此外,您不应在启动期间为 window 调整大小设置动画(因为这在启动时看起来很尴尬)。
func applicationDidFinishLaunching(aNotification: NSNotification) {
if let screen = window.screen ?? NSScreen.mainScreen() {
window.setFrame(screen.visibleFrame, display: true)
}
}
您可能需要考虑使用 NSWindowController
来管理它,而不是将其放在应用程序委托中。在那种情况下,你可以把它放在 windowDidLoad
中。 Window 控制器是 AppKit 中非常常见的工具(与视图控制器相反,视图控制器在历史上并不常见)。
如果您真的想要缩放行为,请熟悉 NSWindowDelegate
方法 windowWillUseStandardFrame(_:defaultFrame:)
。您通常不应该在启动时直接调用 zoom(_:)
,因为这会产生动画效果,但是您在委托中执行的任何逻辑都应该用于计算您的帧。同样,确保将您的框架调整为显示在 window 的屏幕上(如果有的话),而不是显示在主屏幕上。
理想情况下,您确实应该尊重用户使用的最后一帧,而不是将其强制到可见帧。如果您想进一步研究,则在 Cocoa 中称为 frameAutosave
。如果您只是在 Interface Builder 中设置自动保存名称,window 控制器将帮助您自动管理它。 (虽然有点复杂,需要在第一次启动时计算帧以获得可见帧,所以它不会完全自动。)
无论如何,在将默认框架设为可见框架之前,请务必仔细考虑。这在大型显示器上可能真的很大(那里仍然有很多 30 英寸的影院显示器,但即使在 27 英寸的显示器上也可能非常大)。有时这很好,具体取决于您的应用程序,但我经常发现定义最大初始大小(同时允许用户将其变大)是值得的。
大家好,非常感谢您的帮助。
我正在处理基于文档的 mac 应用程序。我将您提供的代码放入文档 class 的 makeWindowControllers() 中,效果非常好。
非常感谢。这是我使用的代码。
override func makeWindowControllers() {
// Returns the Storyboard that contains your Document window.
let storyboard = NSStoryboard(name: "Main", bundle: nil)
let windowController = storyboard.instantiateControllerWithIdentifier("Document Window Controller") as! NSWindowController
self.addWindowController(windowController)
if let screen = NSScreen.mainScreen() {
windowController.window?.setFrame(screen.visibleFrame, display: true, animate: true)
}
}
在 Swift 4.2:
class ViewController: NSViewController {
override func viewDidAppear() {
super.viewDidAppear()
view.window?.zoom(self) //bespread the screen
//view.window?.toggleFullScreen(self) //fullscreen
}
2020 | SWIFT5.1:
使用扩展名:
extension NSWindowController {
func maximize() { self.window?.zoom(self) }
}
只需调用 NSWindowController 实例的 maximize()
:)
Swift 5
如果还有人遇到问题,请尝试在主线程中调用缩放功能。为我工作。
DispatchQueue.main.async {
self.view.window?.zoom(self)
}
此代码仅适用于单一 windowed 应用程序,但编辑起来非常容易用于 multy-windowed 应用程序
使用最大化和非最大化window:
TheApp.maximized.toggle()
源代码
public class TheApp {
static var maximized: Bool {
get {
guard let visibleFrame = NSScreen.main?.visibleFrame,
let window = NSApp.mainWindow
else { return false }
return window.frame == visibleFrame
}
set { NSApp.mainWindow?.zoom(newValue) }
}
static var fullscreen: Bool {
get {
guard let screenFrame = NSScreen.main?.frame,
let window = NSApp.mainWindow
else { return false }
return window.frame == screenFrame
} set {
NSApp.mainWindow?.toggleFullScreen(newValue)
}
}
static var mimimized: Bool {
get { NSApp.mainWindow?.isMiniaturized ?? false }
set { NSApp?.mainWindow?.miniaturize(newValue) }
}
}
我正在开发一个 mac 应用程序,我喜欢使初始 window 处于最大化状态,就像您按下带有加号的绿色按钮时一样。 我不想全屏。
通过使用 NSScreen 的 visibleFrame
作为目标框架,您可以 "zoom" 一个 window 到最大可用 space。假设 window
是您的 NSWindow IBOutlet:
if let screen = NSScreen.mainScreen() {
window.setFrame(screen.visibleFrame, display: true, animate: true)
}
例如,在 AppDelegate.swift:
import Cocoa
@NSApplicationMain
class AppDelegate: NSObject, NSApplicationDelegate {
@IBOutlet weak var window: NSWindow!
func applicationDidFinishLaunching(aNotification: NSNotification) {
if let screen = NSScreen.mainScreen() {
window.setFrame(screen.visibleFrame, display: true, animate: true)
}
}
处于缩放状态的应用程序与 "maximized." 不同,但它几乎可以是任何东西。例如,尝试缩放 Safari window。
假设您真的想要 "maximized" 而不是 "zoom",那么 Eric 就在正确的轨道上,但可以做得更好。首先,你应该使用 window 的屏幕(如果有的话)。此外,您不应在启动期间为 window 调整大小设置动画(因为这在启动时看起来很尴尬)。
func applicationDidFinishLaunching(aNotification: NSNotification) {
if let screen = window.screen ?? NSScreen.mainScreen() {
window.setFrame(screen.visibleFrame, display: true)
}
}
您可能需要考虑使用 NSWindowController
来管理它,而不是将其放在应用程序委托中。在那种情况下,你可以把它放在 windowDidLoad
中。 Window 控制器是 AppKit 中非常常见的工具(与视图控制器相反,视图控制器在历史上并不常见)。
如果您真的想要缩放行为,请熟悉 NSWindowDelegate
方法 windowWillUseStandardFrame(_:defaultFrame:)
。您通常不应该在启动时直接调用 zoom(_:)
,因为这会产生动画效果,但是您在委托中执行的任何逻辑都应该用于计算您的帧。同样,确保将您的框架调整为显示在 window 的屏幕上(如果有的话),而不是显示在主屏幕上。
理想情况下,您确实应该尊重用户使用的最后一帧,而不是将其强制到可见帧。如果您想进一步研究,则在 Cocoa 中称为 frameAutosave
。如果您只是在 Interface Builder 中设置自动保存名称,window 控制器将帮助您自动管理它。 (虽然有点复杂,需要在第一次启动时计算帧以获得可见帧,所以它不会完全自动。)
无论如何,在将默认框架设为可见框架之前,请务必仔细考虑。这在大型显示器上可能真的很大(那里仍然有很多 30 英寸的影院显示器,但即使在 27 英寸的显示器上也可能非常大)。有时这很好,具体取决于您的应用程序,但我经常发现定义最大初始大小(同时允许用户将其变大)是值得的。
大家好,非常感谢您的帮助。
我正在处理基于文档的 mac 应用程序。我将您提供的代码放入文档 class 的 makeWindowControllers() 中,效果非常好。
非常感谢。这是我使用的代码。
override func makeWindowControllers() {
// Returns the Storyboard that contains your Document window.
let storyboard = NSStoryboard(name: "Main", bundle: nil)
let windowController = storyboard.instantiateControllerWithIdentifier("Document Window Controller") as! NSWindowController
self.addWindowController(windowController)
if let screen = NSScreen.mainScreen() {
windowController.window?.setFrame(screen.visibleFrame, display: true, animate: true)
}
}
在 Swift 4.2:
class ViewController: NSViewController {
override func viewDidAppear() {
super.viewDidAppear()
view.window?.zoom(self) //bespread the screen
//view.window?.toggleFullScreen(self) //fullscreen
}
2020 | SWIFT5.1:
使用扩展名:
extension NSWindowController {
func maximize() { self.window?.zoom(self) }
}
只需调用 NSWindowController 实例的 maximize()
:)
Swift 5
如果还有人遇到问题,请尝试在主线程中调用缩放功能。为我工作。
DispatchQueue.main.async {
self.view.window?.zoom(self)
}
此代码仅适用于单一 windowed 应用程序,但编辑起来非常容易用于 multy-windowed 应用程序
使用最大化和非最大化window:
TheApp.maximized.toggle()
源代码
public class TheApp {
static var maximized: Bool {
get {
guard let visibleFrame = NSScreen.main?.visibleFrame,
let window = NSApp.mainWindow
else { return false }
return window.frame == visibleFrame
}
set { NSApp.mainWindow?.zoom(newValue) }
}
static var fullscreen: Bool {
get {
guard let screenFrame = NSScreen.main?.frame,
let window = NSApp.mainWindow
else { return false }
return window.frame == screenFrame
} set {
NSApp.mainWindow?.toggleFullScreen(newValue)
}
}
static var mimimized: Bool {
get { NSApp.mainWindow?.isMiniaturized ?? false }
set { NSApp?.mainWindow?.miniaturize(newValue) }
}
}