OSX app: how to make the window as possible as possible?

I am working on a Mac application and I like the initial window to be in a maximized state, for example, when you press the green button with a plus sign. I do not want it to be fullscreen.

+9
cocoa swift macos nswindow
source share
4 answers

You can β€œenlarge” the window to the maximum available space using the NSScreen visibleFrame as the target frame. Say window is your NSWindow IBOutlet:

 if let screen = NSScreen.mainScreen() { window.setFrame(screen.visibleFrame, display: true, animate: true) } 

For example, in 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) } } 
+6
source share

An application in an enlarged state is not the same as being maximized. A green plus sign means zooming, which means "appropriate size for this content." In some applications, a visible frame (as Eric D. discusses), but that could be just about everything. Try scaling the Safari window, for example.

Assuming you really want to β€œmaximize,” not β€œincrease," then Eric is on the right track, but you can do better. First, you should use a window screen, if any. In addition, you should not animate window resizing during startup (as this may not be convenient at startup).

 func applicationDidFinishLaunching(aNotification: NSNotification) { if let screen = window.screen ?? NSScreen.mainScreen() { window.setFrame(screen.visibleFrame, display: true) } } 

You might want to use the NSWindowController to manage this, rather than putting it in the application delegate. In this case, you can put this in windowDidLoad . Window controllers are a fairly common tool in AppKit (as opposed to view controllers that are not historically common). A.

If you really want to zoom, check out the NSWindowDelegate windowWillUseStandardFrame(_:defaultFrame:) . Usually you do not call zoom(_:) directly at startup, because it will animate, but any logic that you do in the deletion should be used to calculate your frame. Again, do not forget to adjust your frame to live on the window screen, if it has one, and not the main screen.

Ideally, you really should follow the last frame that the user used, and not make him see the visible frame. This is called frameAutosave in Cocoa if you want to explore this more. A window controller will help you manage this somewhat automatically if you just set the autosave name in Interface Builder. (Although this gets a little more complicated when you need to calculate the frame on first run to get a visible frame, so it won't be fully automatic.)

Think carefully before making your default frame a visible frame anyway. It can be really huge on large monitors (there are still many 30-inch Cinema displays, but even on a 27-inch display it can be pretty overwhelming). This is sometimes fine depending on your application, but I often find it worthwhile to determine the maximum initial size (allowing the user to make it larger).

+9
source share

Hi guys, I really appreciate your help.

I am working on a document based Mac. I put the code that you provided in the makeWindowControllers () of the Document class and it works like a charm.

Many thanks. Here is the code I'm using.

 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) } } 
0
source share

in Swift 4.2:

 class ViewController: NSViewController { override func viewDidAppear() { super.viewDidAppear() view.window?.zoom(self) //bespread the screen //view.window?.toggleFullScreen(self) //fullscreen } 
0
source share

All Articles