如何在 Swift (MacOS) 中绘制无边框矩形

问题描述 投票:0回答:1

我当前的代码非常基础,因为我对 Swift 还很陌生。但是,我不知道如何在屏幕上绘制一个没有边框的矩形(关闭、最小化、放大按钮),如下图所示:

(另外请告诉我那个边框实际上叫什么!)

import SwiftUI

struct ContentView: View {
    var body: some View {
        Rectangle()
            .fill(Color.red)
            .frame(width: 200, height: 200)
    }
}

无论如何,我习惯在 Garry's Mod 中使用 Lua 进行编码,通过定义矩形的坐标和大小,我可以在屏幕上的任何位置绘制矩形,所以我只是想知道是否可以这样做在 Swift 上也是如此,或者也许其他语言会更有益?

提前致谢!

swift macos user-interface swiftui terminology
1个回答
1
投票

带有按钮的标题是窗口的一部分,而不是绘制矩形,因此您只需创建另一种类型的窗口(没有标题和按钮),这样您就可以成为您的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)
}
© www.soinside.com 2019 - 2024. All rights reserved.