OSX Application: how to make window maximized?

2020 | SWIFT 5.1:

use extension:

extension NSWindowController {
    func maximize() { self.window?.zoom(self) }
}

just call maximize() of NSWindowController instance :)


You can "zoom" a window to the max available space by using NSScreen's visibleFrame as the target frame. Let's say window is your NSWindow IBOutlet:

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

For example, in the 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)
        }
    }

in Swift 4.2:

class ViewController: NSViewController {
 override func viewDidAppear() {
    super.viewDidAppear()
    view.window?.zoom(self) //bespread the screen

  //view.window?.toggleFullScreen(self)  //fullscreen
}

An app in its zoomed state is not the same thing as "maximized." The green plus icon indicates zoom, which means "the appropriate size for this content." In some applications that's the visible frame (as Eric D. discusses), but it can be almost anything. Try zooming a Safari window for instance.

Assuming you really want "maximized" and not "zoom", then Eric is on the right track, but it can be done better. First, you should use the window's screen if it has one. Also, you should not animate the window resize during launch (since that can look awkward on launch).

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

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

If you actually want zoom behavior, familiarize yourself with the the NSWindowDelegate method windowWillUseStandardFrame(_:defaultFrame:). You shouldn't generally call zoom(_:) directly on launch because that will animate, but whatever logic you do in the delegate should be used to compute your frame. Again, make sure to adjust your frame to live on the window's screen if it has one, rather than the main screen.

Ideally, you really should be honoring the last frame that the user used rather than forcing it to the visible frame. That's called frameAutosave in Cocoa if you want to research that more. A window controller will help you manage that somewhat automatically if you just set a autosave name in Interface Builder. (Though it's slightly complicated by needing to compute the frame on first launch to get the visible frame, so it won't be completely automatic.)

Do give some careful thought before making your default frame be the visible frame in any case. That can be really enormous on large monitors (there are still a lot of 30" Cinema displays out there, but even on a 27" it can be pretty overwhelming). Sometimes that's fine depending on your app, but I often find that it's worth defining a maximum initial size (while allowing the user to make it larger).