0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

MacApp を シングルウインドウ にする方法

Posted at

いつも忘れてしまっていて、都度、ググる羽目になるので、備忘録としてまとめておきます。

SampleApp.swift
import SwiftUI

class AppDelegate: NSObject, NSApplicationDelegate, NSWindowDelegate {
    func applicationShouldTerminateAfterLastWindowClosed(_ sender: NSApplication) -> Bool {
        return true //⭐️ true: 最後のウインドウを閉じたら終了する
    }
}

@main
struct SampleApp: App {
    @NSApplicationDelegateAdaptor(AppDelegate.self) var appDelegate //⭐️
    var body: some Scene {
        WindowGroup {
            ContentView()
        }
    }
}

普通にアプリを作ると、ウインドウを閉じてもアプリは終了しません。
マルチウインドウ ならそれでもよいですが、シングルウインドウ アプリの場合は、アプリを終了してほしい場合があります。

その場合は、AppDelegateapplicationShouldTerminateAfterLastWindowClosed(_:)を実装することで実現できます。

以上

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?