Set the size and the location of the window in the AppKit apps | How To Create macOS Apps

This article is part of a series of articles that create the Cocoa Hello World. This article explains how to set the minimum size and the maximum size of the window. It also add the code to centering the window to HelloWindowController class.

This article uses the sample code which was created in the previous article. You can download it from the following article.

TOC

Set the minimum size and the maximum size of the window

There are following ways that to set the minimum size and the maximum size of the window.

  • By the code
  • Configure in the storyboard file

This article explains the code. Why we chose the way that by code? Because,

  • Easy to trial and errors to decide the value.
  • If you change the value in the future, easy to track the changes.

Add the code

When the storyboard file is loaded, AppKit call the NSWindowController.windowDidLoad() method. The windowDidLoad() method is a delegate method which is for initializing the window. Set the minimum size and the maximum size in this method.

Add the code as follows.

import Cocoa

class HelloWindowController: NSWindowController {
    
    let minWindowWidth: CGFloat = 200
    let minWindowHeight: CGFloat = 150
    let maxWindowWidth: CGFloat = 1200
    let maxWindowHeight: CGFloat = 900

    override func windowDidLoad() {
        super.windowDidLoad()
        
        window?.minSize = CGSize(width: minWindowWidth,
                                 height: minWindowHeight)
        window?.maxSize = CGSize(width: maxWindowWidth,
                                 height: maxWindowHeight)
    }

}

The following screen shot is a screen shot when the window is minimum size.

Window at minimum size
Window at minimum size

Get the window

The instance of the window is in the NSWindowController.window property.

open var window: NSWindow?

Minimum size

Set the minimum size of the window to NSWindow.minSize property.

open var minSize: NSSize

Maximum size

Set the maximum size of the window to NSWindow.maxSize property.

open var maxSize: NSSize

Adjust the cursor

When you move the cursor to the corner or frame of the resizable window, the cursor is changed automatically to indicate the window is resizable. AppKit provides standard macOS cursor control without having to do anything.

For example, the cursor of the following screen shot indicates that you can both expanding or shrinking the window.

You can expand or shrink
You can expand or shrink

When the window at minimum size, the cursor indicates you can expand only.

You can expand only
You can expand only

When the window at maximum size, the cursor indicates that you can shrink only.

You can shrink only
You can shrink only

Centering the window

Add the code that centering the window.

Add the code

Simply think, to center the window, you need to calculate the location with the screen size and the window size.

However, the NSWindow class has a method to move the window to center of the screen, so you can center the window by simply use this method.

Change the code as follows.

import Cocoa

class HelloWindowController: NSWindowController {
    
    let minWindowWidth: CGFloat = 200
    let minWindowHeight: CGFloat = 150
    let maxWindowWidth: CGFloat = 1200
    let maxWindowHeight: CGFloat = 900

    override func windowDidLoad() {
        super.windowDidLoad()
        
        window?.minSize = CGSize(width: minWindowWidth,
                                 height: minWindowHeight)
        window?.maxSize = CGSize(width: maxWindowWidth,
                                 height: maxWindowHeight)

        window?.makeKeyAndOrderFront(nil)
        window?.center()
    }

}

Added code is following.

window?.makeKeyAndOrderFront(nil)
window?.center()

About the window list and the key window

macOS has a window list which manages the displayed window in the screen.

While windowDidLoad() method is running, the window is not in the window list so it is not displayed yet.

At this time, the center() method can’t center the window because the screen is not decided.

So, we use NSWindow.makeKeyAndOrderFront() method. The makeKeyAndOrderFront() method do following.

  • Move the window to the front of the screen.
  • Make the window to a key window.
  • Show the window.

What is key window?

Multiple apps in a screen at same time, and each apps has multiple windows. When you press the key, the front most window receives the key down event and process it. AppKit calls this window as a key window.

Not center?

Did you found that when you center the window with center() method, but the window is not actually center. It is located at the slightly above.

This is not a bug.

This specification is made because human vision is more likely to see and notice a position that is slightly off center than perfectly centered.

Download the sample code

The sample code was created in this article can be downloaded from here.

List of Serialized Articles

This article is part of a series of articles titled “How to create macOS Apps with AppKit”. For other articles, please open the following links.

How to create macOS apps with AppKit List of Serialized Articles

Let's share this post !

Author of this article

Akira Hayashiのアバター Akira Hayashi Representative, Software Engineer

I am an application developer loves programming. This blog is a tech blog, its articles are learning notes. In my work, I mainly focus on desktop and mobile application development, but I also write technical books and teach seminars. The websites of my work and books are here -> RK Kaihatsu.

TOC