I am having a hard time to figure out why my popover window is not transparent. You can see in the image bellow. popover

我正在试着让黑色的正方形和周围的 colored颜色 一样(透明).

我也try 了这些NSWindow风格,但没有成功.似乎无论我发现或try 什么,都不会发生什么,也不会改变事实.如有任何帮助或提示,我将不胜感激.谢谢

https://github.com/lukakerr/NSWindowStyles

以下是我的代码:

import SwiftUI

@main
struct GPTbarApp: App {
    
    @NSApplicationDelegateAdaptor(AppDelegate.self) private var appDelegate
    
    var body: some Scene {
        WindowGroup {
            ContentView()
        }
    }
}

class AppDelegate: NSObject, NSApplicationDelegate {
    
    private var statusItem: NSStatusItem!
    private var popover: NSPopover!
    
    @MainActor func applicationDidFinishLaunching(_ notification: Notification) {
        statusItem = NSStatusBar.system.statusItem(withLength: NSStatusItem.variableLength)
        
        if let statusButton = statusItem.button {
            statusButton.image = NSImage(systemSymbolName: "info.circle.fill", accessibilityDescription: "Information")
            statusButton.action = #selector(togglePopover)
        }
        
        self.popover = NSPopover()
        self.popover.contentSize = NSSize(width: 300, height: 300)
        self.popover.behavior = .transient
        
        let hostingController = NSHostingController(rootView: ContentView())
        hostingController.view.wantsLayer = true
        hostingController.view.layer?.backgroundColor = NSColor.clear.cgColor // Set the layer's background color to clear
        
        self.popover.contentViewController = hostingController
    }
    
    @objc func togglePopover() {
        if let button = statusItem.button {
            if popover.isShown {
                self.popover.performClose(nil)
            } else {
                popover.show(relativeTo: button.bounds, of: button, preferredEdge: .minY)
            }
        }
    }
}

内容视图:

import SwiftUI
import OpenAISwift

struct 内容视图: View {
    
    @State private var search: String = ""
    @State private var responses: [String] = []
    
    let openAI = OpenAISwift(authToken: "sk-")
    
    private var isFormValid: Bool {
        !search.isEmpty
    }
    
    private func performSearch() {
        responses.append("You: \(search)")
        
        openAI.sendCompletion(with: search, maxTokens: 500) { result in
            switch result {
            case .success(let success):
                let response = "\(success.choices?.first?.text ?? "")"
                responses.append(response)
            case .failure(let failure):
                print(failure.localizedDescription)
            }
        }
    }
    
    var body: some View {
        VStack {
            Spacer()
            
            List(responses, id: \.self) { response in
                Text(response)
                    .background(Color.clear) // Set the background color of the text to clear
            }
            .listStyle(.plain) // Set the list style to plain
            
            HStack {
                TextField("Search.", text: $search)
                    .textFieldStyle(.roundedBorder)
                
                Button {
                    performSearch()
                } label: {
                    Image(systemName: "magnifyingglass.circle.fill")
                        .font(.title)
                }
                .buttonStyle(.borderless)
                .disabled(!isFormValid)
            }
        }
        .padding()
    }
}

struct ContentView_Previews: PreviewProvider {
    static var previews: some View {
        ContentView()
    }
}

推荐答案

macOS 13.0+,用这句话来形容你的List:

List(responses, id: \.self) { response in
   Text(response)
}
.listStyle(.plain)
.scrollContentBackground(.hidden)

Swift相关问答推荐

为什么在Swift属性Wrapper中需要类型注释?

格式化单位和度量值.为什么从符号初始化的单位不能产生与静态初始化相同的结果

正在接收具有不完整数据错误的URL请求

异步/等待函数的XCTAssertThrowsError

以编程方式使UIView居中,而不影响UIKit中其他UI类的位置

BLE 在 ESP32 和 Iphone 之间发送字符串

允许为 self 分配新的 struct 值的理由是什么?

当字符串包含 \r\n 时,NSRegularExpression 不起作用

如何在 swiftui 中设置给定字符串类型日期的日期倒计时?

从 Swift Vapor Server 中调用 ShazamKit

SwiftUI:决定键盘重叠的内容

SwiftUI View Builder 参数无法更改状态

如何更改 Picker 的边框 colored颜色

在 RealityKit 中创建转场

swiftui更改显示的 Select 器值

单元测试和私有变量

Swift 2.0 方法不能标记为@objc,因为参数的类型不能在 Objective-C 中表示

Swift 2 或 3 中的 Google Analytics 问题

swift - 我可以从我的自定义初始化方法中调用 struct 默认成员初始化吗?

如何将 Int 拆分为其各个数字?