【问题标题】:How to have a fully functioning NavigationView in a .fullSizeContent window with SwiftUI如何使用 SwiftUI 在 .fullSizeContent 窗口中拥有功能齐全的 NavigationView
【发布时间】:2020-10-04 05:02:41
【问题描述】:

我正试图让我的NavigationView 填满整个窗口。 List 应该从其窗口的顶部边缘延伸到底部边缘。但是,虽然列表一开始看起来是正确的,将.fullSizeContentView 添加到NSWindowstyleMask,但只要我选择一个列表项,整个 NavigationView 就会缩小。

为了防止缩水,我找到了两个选项。

  1. ZStackColor-background 一起使用,这样背景将调整DetailView 的大小。
  2. 使用frame-修饰符,其中maxWidthmaxHeight.infinity

这两个选项都会破坏List 的箭头键导航。

使用NavigationView 设置全尺寸内容的正确方法是什么?

以下是复制问题所需的完整工作示例代码。如果您想对其进行测试,只需在 Catalina 上的全新 Swift macOS 应用程序中替换这些文件的内容即可。

AppDelegate.swift

import Cocoa
import SwiftUI

@NSApplicationMain
class AppDelegate: NSObject, NSApplicationDelegate {
    var window: NSWindow!

    func applicationDidFinishLaunching(_ aNotification: Notification) {
        let contentView = ContentView()

        window = NSWindow(
            contentRect: NSRect(x: 0, y: 0, width: 480, height: 300),
            styleMask: [.titled, .closable, .miniaturizable, .resizable, .fullSizeContentView],
            // <---- remove .fullSizeContentView mask and layout will work without option 1 or 2 in ContentView
            backing: .buffered, defer: false)
        window.titlebarAppearsTransparent = true
        window.titleVisibility = .hidden
        window.isReleasedWhenClosed = false
        window.center()
        window.setFrameAutosaveName("Main Window")
        window.contentView = NSHostingView(rootView: contentView)
        window.makeKeyAndOrderFront(nil)
    }
}

ContentView.swift

import SwiftUI

let warners = ["Yacko","Wacko","Dot"]

struct WarnerView: View {
    var name: String
    var body: some View {
//        ZStack { // <---- Option 1: use ZStack with Color.clear
//            Color.clear
            VStack {
                Text(name)
                Text("Nothing to see here!")
            }
//            .frame(minWidth: 100, idealWidth: 100, maxWidth: .infinity, minHeight: 100, idealHeight: 100, maxHeight: .infinity, alignment: .center)
//          <---- Option 2: use only VStack with frame
//        } // <- closing bracket for ZStack
    }
}

struct ContentView: View {
    var body: some View {
        ZStack {
            Color.clear
            NavigationView {
                VStack(alignment: .leading) {
                    HStack {
                        Text("Warners")
                            .font(.headline)
                    }
                        .padding(16)
                    List(warners, id: \.self)
                     { warner in
                        NavigationLink(destination: WarnerView(name: warner)
                        ) {
                            Text(warner)
                        }
                    }.listStyle(SidebarListStyle())
                }
            }
        }
        .edgesIgnoringSafeArea(.all)
        .frame(minWidth: 600, minHeight: 400)
    }
}

【问题讨论】:

    标签: swift swiftui macos-catalina navigationview arrow-keys


    【解决方案1】:

    据我所知,这是一种方式(使用 Xcode 12 / macOS 10.15.6 测试)。你上面的WarnerView没有变化。

    struct ContentView: View {
        var body: some View {
                NavigationView {
                    List {
                      HStack {
                            Text("Warners")
                                 .font(.headline)
                      }
                      ForEach(warners, id: \.self)
                        { warner in
                            NavigationLink(destination:
                                WarnerView(name: warner)
                                    .frame(maxWidth: .infinity, maxHeight: .infinity)
                                    ) {
                                 Text(warner)
                            }
                      }
                    }.listStyle(SidebarListStyle())
    
    // uncomment below for default details area
    //                  WarnerView(name: "")
    //                      .frame(maxWidth: .infinity, maxHeight: .infinity)
            }
        }
    }
    

    【讨论】:

    • 感谢您的回答。但我猜这缺少 AppDelegate.swift 中的 .fullSizeContentView。所以隐藏的标题栏是可见的。
    • 缺少 .fullSizeContentView - 不,.fullSizeContentView 存在。如果您想隐藏不同问题的标题栏 - 请考虑stackoverflow.com/a/60252103/12299030
    • 我认为这不是一个不同的问题,因为我提出了这个问题:I'm trying to get my NavigationView to fill the whole window. The List should stretch from the top to bottom edge of its window. 我确实按照您通过链接看到的答案中的建议设置了.edgesIgnoringSafeArea(.all)。只有我在 ContentView.swift 中设置它,而不是在 AppDelegate 中。将其移至 AppDelegate 似乎对问题没有任何影响。该列表不能使用箭头键导航。
    猜你喜欢
    • 1970-01-01
    • 2017-06-18
    • 2021-12-15
    • 2019-10-30
    • 2020-12-11
    • 1970-01-01
    • 1970-01-01
    • 2011-10-20
    • 1970-01-01
    相关资源
    最近更新 更多