Управление положением окон на мак в SwiftUI

Asked on 01/30/2025

1 search

To manage window positioning on macOS using SwiftUI, you can utilize the default window placement modifier. This allows you to programmatically set the initial position and size of a window. Here's a brief overview of how you can achieve this:

  1. Calculate Position: You can calculate a position for the window manually by accessing the context, which contains information about the default display. This includes the visible rect, representing where it is safe to place content. You can then use this information to determine the window's position and size.

  2. Default Window Placement API: This API allows you to specify an initial size and position for your window. You can use it to ensure that your window is placed in a comfortable position on the screen, such as centered horizontally and just above the bottom of the display.

  3. Customization: You can further customize the window's behavior by changing how it can be resized or by using different scene types like utility windows.

For more detailed guidance, you can refer to the session Work with windows in SwiftUI (06:15) which covers window placement in detail.