最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

swift - NavigationView Toolbar Button Not Visible and Window Style Issue When Using a Main File Instead of @main - Stack Overflo

programmeradmin4浏览0评论

I am using the same ContentView once with @main and once with a main file. When I use the main file, I am unable to see the plus button. Also, the style of the window is different in each case. I want the same window style as the @main method, and I want my plus button to be visible. How can I solve this problem? I see a padding in Window buttons in @main.

import SwiftUI

@main
struct TestApp: App {

    var body: some Scene {
        WindowGroup("Test") {
            ContentView()
        }
    }
}

struct ContentView: View {

    var body: some View {
        NavigationView {
            List {
                Text("Item 1")
                Text("Item 2")
                Text("Item 3")
            }
            .toolbar {
                ToolbarItem {
                    Button(action: { print("action") }) {
                        Label("Add Item", systemImage: "plus")
                    }
                }
            }
            Text("Select an item")
        }
    }

}


import Cocoa
import SwiftUI

class AppDelegate: NSObject, NSApplicationDelegate {
    
    private lazy var window: NSWindow = NSWindow()
    
    func applicationDidFinishLaunching(_ aNotification: Notification) {

        window.styleMask = [.titled, .closable, .miniaturizable, .resizable, .fullSizeContentView]
        window.backingType = .buffered
        window.isReleasedWhenClosed = false
        window.center()
        window.setFrameAutosaveName("Main Window")

        window.title = "Test"
        window.isMovableByWindowBackground = true
        
        window.contentView = NSHostingView(rootView: ContentView())
        window.makeKeyAndOrderFront(window)
        
    }

    
}


struct ContentView: View {

    var body: some View {
        NavigationView {
            List {
                Text("Item 1")
                Text("Item 2")
                Text("Item 3")
            }
            .toolbar {
                ToolbarItem {
                    Button(action: { print("action") }) {
                        Label("Add Item", systemImage: "plus")
                    }
                }
            }
            Text("Select an item")
        }
    }

}


与本文相关的文章

发布评论

评论列表(0)

  1. 暂无评论