Remember to maintain security and privacy. Do not share sensitive information. Procedimento.com.br may make mistakes. Verify important information. Termo de Responsabilidade

How to Create a Window in macOS Using SwiftUI

Creating windows in macOS applications is a fundamental skill for developers who want to build graphical user interfaces. In the Apple environment, SwiftUI is a powerful and modern framework that allows developers to create user interfaces across all Apple platforms using Swift. This article will guide you through the process of creating a window in a macOS application using SwiftUI. We will cover the importance of this task, provide practical examples, and explain the necessary adjustments to align with the Apple environment.

Examples:

Example 1: Basic Window Creation with SwiftUI

To create a basic window in a macOS application using SwiftUI, follow these steps:

  1. Open Xcode and create a new macOS project.
  2. Choose "App" as the template and click "Next".
  3. Name your project and ensure the language is set to Swift.
  4. Replace the content of ContentView.swift with the following code:
import SwiftUI

struct ContentView: View {
    var body: some View {
        Text("Hello, World!")
            .frame(maxWidth: .infinity, maxHeight: .infinity)
    }
}

@main
struct MyApp: App {
    var body: some Scene {
        WindowGroup {
            ContentView()
        }
    }
}

This code creates a simple window displaying "Hello, World!" using SwiftUI.

Example 2: Creating Multiple Windows

To create multiple windows in a macOS application, you can use the WindowGroup and Window modifiers. Here’s how:

  1. Open your existing project or create a new one.
  2. Update MyApp.swift with the following code:
import SwiftUI

struct ContentView: View {
    var body: some View {
        Text("Main Window")
            .frame(maxWidth: .infinity, maxHeight: .infinity)
    }
}

struct SecondaryView: View {
    var body: some View {
        Text("Secondary Window")
            .frame(maxWidth: .infinity, maxHeight: .infinity)
    }
}

@main
struct MyApp: App {
    var body: some Scene {
        WindowGroup {
            ContentView()
        }
        Window("Secondary Window", id: "secondary") {
            SecondaryView()
        }
    }
}

This code creates two separate windows: one displaying "Main Window" and the other displaying "Secondary Window".

Example 3: Customizing Window Properties

You can customize the properties of your windows, such as size and title, using the WindowGroup and Window modifiers. Here’s an example:

import SwiftUI

struct ContentView: View {
    var body: some View {
        Text("Custom Window")
            .frame(maxWidth: .infinity, maxHeight: .infinity)
    }
}

@main
struct MyApp: App {
    var body: some Scene {
        WindowGroup {
            ContentView()
        }
        .windowStyle(HiddenTitleBarWindowStyle())
        .windowToolbarStyle(UnifiedCompactWindowToolbarStyle())
        .commands {
            CommandGroup(replacing: .newItem) {
                Button("New Window") {
                    NSApplication.shared.keyWindow?.close()
                }
                .keyboardShortcut("N", modifiers: [.command])
            }
        }
    }
}

This code customizes the window to have a hidden title bar and a unified compact toolbar style, and adds a command to close the window using a keyboard shortcut.

To share Download PDF

Gostou do artigo? Deixe sua avaliação!
Sua opinião é muito importante para nós. Clique em um dos botões abaixo para nos dizer o que achou deste conteúdo.