如何在Swift(MacOS)中绘制无边框的矩形
我目前的代码非常基本,因为我对Swift还是新手。然而,我不知道如何在屏幕上画一个没有边框(关闭、最小化、放大按钮)的矩形,就像链接的图片一样 (也请告诉我边界的实际名称!) 无论如何,我已经习惯了用Lua为Garry的Mod编写代码,通过定义矩形的坐标和大小,我可以在屏幕上任意位置绘制矩形,所以我只是想知道是否可以在Swift上也这样做,或者其他语言会更有用如何在Swift(MacOS)中绘制无边框的矩形,swift,macos,swiftui,Swift,Macos,Swiftui,我目前的代码非常基本,因为我对Swift还是新手。然而,我不知道如何在屏幕上画一个没有边框(关闭、最小化、放大按钮)的矩形,就像链接的图片一样 (也请告诉我边界的实际名称!) 无论如何,我已经习惯了用Lua为Garry的Mod编写代码,通过定义矩形的坐标和大小,我可以在屏幕上任意位置绘制矩形,所以我只是想知道是否可以在Swift上也这样做,或者其他语言会更有用 提前谢谢 带按钮的标题是窗口的一部分,而不是绘制矩形,因此您只需创建另一种类型的窗口(不带标题和按钮),就可以创建AppDelegat
提前谢谢 带按钮的标题是窗口的一部分,而不是绘制矩形,因此您只需创建另一种类型的窗口(不带标题和按钮),就可以创建AppDelegate
func applicationDidFinishLaunching(_ aNotification: Notification) {
// Create the SwiftUI view that provides the window contents.
let contentView = ContentView()
.edgesIgnoringSafeArea(.top) // to extend entire content under titlebar
// Create the window and set the content view.
window = NSWindow(
contentRect: NSRect(x: 0, y: 0, width: 480, height: 300),
styleMask: [.titled, .texturedBackground, .fullSizeContentView],
backing: .buffered, defer: false)
window.center()
window.setFrameAutosaveName("Main Window")
window.titlebarAppearsTransparent = true // as stated
window.titleVisibility = .hidden // no title - all in content
window.contentView = NSHostingView(rootView: contentView)
window.makeKeyAndOrderFront(nil)
}
带按钮的标题是窗口的一部分,而不是绘制矩形,所以您只需创建另一种类型的窗口(不带标题和按钮),就可以创建AppDelegate
func applicationDidFinishLaunching(_ aNotification: Notification) {
// Create the SwiftUI view that provides the window contents.
let contentView = ContentView()
.edgesIgnoringSafeArea(.top) // to extend entire content under titlebar
// Create the window and set the content view.
window = NSWindow(
contentRect: NSRect(x: 0, y: 0, width: 480, height: 300),
styleMask: [.titled, .texturedBackground, .fullSizeContentView],
backing: .buffered, defer: false)
window.center()
window.setFrameAutosaveName("Main Window")
window.titlebarAppearsTransparent = true // as stated
window.titleVisibility = .hidden // no title - all in content
window.contentView = NSHostingView(rootView: contentView)
window.makeKeyAndOrderFront(nil)
}
这取决于
NSWindow
。请参阅创建macOS应用程序。这取决于NSWindow
。请参阅创建macOS应用程序。你知道在Xcode 12中使用SwiftUI生命周期时如何更改窗口样式掩码吗?@LeoDabus,有一些想法,但这是不同的主题。我找不到任何选项来删除标题栏或禁用最小化或缩放。只有WindowGroup{ContentView()}.WindowsStyle(HiddentileBarWindowsStyle())
你知道在Xcode 12中使用SwiftUI生命周期时如何更改窗口样式掩码吗?@LeoDabus,有一些想法,但这是不同的主题。我找不到任何选项来删除标题栏或禁用最小化或缩放。仅WindowGroup{ContentView()}.windowStyle(HiddenTitleBarWindowStyle())