如何在SwiftUI中禁用显示选项卡栏菜单选项 [英] How do I disable the Show Tab Bar menu option in SwiftUI
问题描述
我已经在使用SwiftUI创建的MacOS上创建了一个非常简单的应用程序.默认情况下,有一个菜单项显示选项卡栏
.我该如何删除?我在此应用中没有标签页是没有道理的.
我发现以下回答相同的问题,但是对于旧版本的Swift,而不是SwiftUI:
在 NSWindow
上有一个名为 tabbingMode
的属性,该属性允许我们通过将其设置为 .disallowed
来进行控制.但是我的问题是:在SwiftUI 2生命周期应用程序中,我如何掌握该应用程序的窗口?
好吧,有 NSApplication.shared.windows
,所以我的第一个(不工作!)尝试是修改我 @main
-App结构(因为我已经阻止了创建新窗口,这已经足够了):
import SwiftUI@主要的struct DQ_SyslogApp:应用{var主体:某些场景{WindowGroup {ContentView().onAppear {让_ = NSApplication.shared.windows.map {$ 0.tabbingMode = .disallowed}}}.commands {CommandGroup(replaceing:.newItem){}//删除新建项"-菜单项}}}
不幸的是,此操作不起作用,因为 .onAppear
中的 NSApplication.shared.windows
为空.
我的下一步涉及向我的SwiftUI 2生命周期引入一个AppDelegate,该实现实现 applicationDidFinishLaunching(_:)
...
class AppDelegate:NSObject,NSApplicationDelegate {func applicationDidFinishLaunching(_通知:通知){打印(来自applicationDidFinishLaunching(_ :)的信息:启动完成…")让_ = NSApplication.shared.windows.map {$ 0.tabbingMode = .disallowed}}}
...并将此AppDelegate引入应用:
import SwiftUI@主要的struct DQ_SyslogApp:应用{@NSApplicationDelegateAdaptor(AppDelegate.self)var appDelegatevar主体:某些场景{WindowGroup {ContentView()}.commands {CommandGroup(replaceing:.newItem){}//删除新建项"-菜单项}}}
这对我有用:
虽然我希望完全删除菜单项,但这至少可以防止用户拥有OP所要求的选项卡.
如果有人碰巧知道隐藏这些条目的解决方案,请告诉我们.我找不到代表这些菜单的 CommandGroupPlacement
...
I have created a very simple app on MacOS created with SwiftUI. Default there is a menu item show tab bar
. How do I remove this? I doesn't make sense to have tabs in this app.
I've found the following answering the same question, but for older versions of Swift, not for SwiftUI: How do I disable the Show Tab Bar menu option in Sierra apps?
I was looking for an answer for this as well and found out the following:
by default - as you already mentioned - the Show/Hide Tab is active:
There is a property on NSWindow
called tabbingMode
which allows us to take control by setting it to .disallowed
. My problem though was: in a SwiftUI 2-lifecycle app, how can I get hold of the windows of the app?
Well, there's NSApplication.shared.windows
, so my first (non working!!) attempt was to modify all the windows in my @main
-App struct (as I already prevented new windows from being created, that should be suffice):
import SwiftUI
@main
struct DQ_SyslogApp: App {
var body: some Scene {
WindowGroup {
ContentView()
.onAppear {
let _ = NSApplication.shared.windows.map { $0.tabbingMode = .disallowed }
}
}
.commands {
CommandGroup(replacing: .newItem) {} //remove "New Item"-menu entry
}
}
}
Unfortunately this did not work as NSApplication.shared.windows
is empty in .onAppear
.
My next step involved introducing an AppDelegate to my SwiftUI 2-lifecycle that implements applicationDidFinishLaunching(_:)
...
class AppDelegate: NSObject, NSApplicationDelegate {
func applicationDidFinishLaunching(_ notification: Notification) {
print("Info from `applicationDidFinishLaunching(_:): Finished launching…")
let _ = NSApplication.shared.windows.map { $0.tabbingMode = .disallowed }
}
}
...and introducing this AppDelegate to the app:
import SwiftUI
@main
struct DQ_SyslogApp: App {
@NSApplicationDelegateAdaptor(AppDelegate.self) var appDelegate
var body: some Scene {
WindowGroup {
ContentView()
}
.commands {
CommandGroup(replacing: .newItem) {} //remove "New Item"-menu entry
}
}
}
This did the trick for me:
While I would prefer to have the menu entries removed entirely, this at least prevents the user from having tabs which is what the OP was asking for.
If anybody should happen to know a solution to hide those entries, please let us know. I couldn't find a CommandGroupPlacement
that represents these menus...
这篇关于如何在SwiftUI中禁用显示选项卡栏菜单选项的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!