Swiftui:在单击应用程序的码头图标时,如何使您的应用程序进入前面?
当将SwiftUI应用程序最小化并单击码头图标时。该应用不会像其他应用程序那样被杀死并将其放在前面。
import SwiftUI
@main
struct MyApp: App {
@NSApplicationDelegateAdaptor(AppDelegate.self) var appDelegate
var body: some Scene {
WindowGroup {
MainView()
}
}
}
class AppDelegate: NSObject, NSApplicationDelegate {
func applicationShouldHandleReopen(_ sender: NSApplication, hasVisibleWindows flag: Bool) -> Bool {
// THIS IS NEVER CALLED!!!
if !flag {
for window: AnyObject in sender.windows {
window.makeKeyAndOrderFront(self)
}
}
return true
}
}
其他委托方法(例如ApplicationDidLaunch)确实被调用,因此它不是链接问题。有人知道如何使它起作用吗?
When a SwiftUI app is minimized and the dock icon is clicked. The app won't be deminimized and put to the front just like other apps do.
import SwiftUI
@main
struct MyApp: App {
@NSApplicationDelegateAdaptor(AppDelegate.self) var appDelegate
var body: some Scene {
WindowGroup {
MainView()
}
}
}
class AppDelegate: NSObject, NSApplicationDelegate {
func applicationShouldHandleReopen(_ sender: NSApplication, hasVisibleWindows flag: Bool) -> Bool {
// THIS IS NEVER CALLED!!!
if !flag {
for window: AnyObject in sender.windows {
window.makeKeyAndOrderFront(self)
}
}
return true
}
}
Other delegate methods like applicationDidLaunch do get called so its not a linking issue. Does anyone know how to get this to work?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
2022
将显示应用程序被隐藏或最小化的应用程序!
2022
Will display app in case of it is hidden or minimized both!
在单击Dock图标时被调用的委托方法,请尝试调用
此应用程序应将您的应用程序带到前景
In delegate methods that get called when clicking on the dock icon try calling
This should bring your app to the foreground
使用确实成为主动回调,被测试为Xcode 13.3/MacOS 12.2.1
类似,
项目中的测试模块在此处
注意:如果有活动应用程序,则在单击dock图标中唯一的
application(will/dod)更新
。Use did become active callback, tested as worked with Xcode 13.3 / macOS 12.2.1
Like,
Test module in project is here
Note: in case of active application the only
application(Will/Did)Update
are called in click Dock icon.我找到了这个解决方案。
在您的@Main输入类应用程序中添加此属性:
现在应该看起来像这样:
现在,创建AppDelegate类(您可以创建一个新的独立的AppDelegate.swift文件,或者在App struct关闭后继续键入:
现在它正在最小化,最小化, 最大化,关闭和重新开放(Ex。Safari,Mail等)。
与其他应用程序一样,
I found this solution.
In your @main entry class App add this property:
It now should looks like this:
Now, create the AppDelegate class (you can create a new separate AppDelegate.swift file or just continue typing after the App struct closed:
Now it is minimizing, maximizing, closing and reopening again as normal as other apps (ex. Safari, Mail, etc.).
This works perfectly on Monterey 12.4.
这是围绕我有用的临时工作
}
This is a temporary work around that works for me
}