gpt4 book ai didi

macos - 在 SwiftUI macOS 应用程序中根据 NavigationView 更改窗口大小

转载 作者:行者123 更新时间:2023-12-03 22:01:41 25 4
gpt4 key购买 nike

我正在将 SwiftUI 用于主窗口包含 NavigationView 的 Mac 应用程序。此 NavigationView 包含一个侧边栏列表。选择侧栏中的项目时,它会更改详细 View 中显示的 View 。详细 View 中显示的 View 大小不同,这应该会导致窗口大小在显示时发生变化。但是,当详细 View 更改大小时,窗口不会更改大小以适应新的详细 View 。

如何根据 NavigationView 的大小更改窗口大小?

我的应用程序示例代码如下:

import SwiftUI

struct View200: View {
var body: some View {
Text("200").font(.title)
.frame(width: 200, height: 400)
.background(Color(.systemRed))
}
}

struct View500: View {
var body: some View {
Text("500").font(.title)
.frame(width: 500, height: 300)
.background(Color(.systemBlue))
}
}

struct ViewOther: View {
let item: Int
var body: some View {
Text("\(item)").font(.title)
.frame(width: 300, height: 200)
.background(Color(.systemGreen))
}
}

struct DetailView: View {

let item: Int

var body: some View {
switch item {
case 2:
return AnyView(View200())
case 5:
return AnyView(View500())
default:
return AnyView(ViewOther(item: item))
}
}
}

struct ContentView: View {
var body: some View {
NavigationView {
List {
ForEach(1...10, id: \.self) { index in
NavigationLink(destination: DetailView(item: index)) {
Text("Link \(index)")
}
}
}
.listStyle(SidebarListStyle())
}
.frame(maxWidth: .infinity, maxHeight: .infinity)
}
}

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

这是当详细 View 更改大小时示例应用程序的样子:

screen recording

最佳答案

这是可行的可能方法的演示。我是从一种不同的观点来做的,因为你需要重新设计你的解决方案才能采用它。

演示

SwiftUI animate window frame

1) 需要窗口动画调整大小的 View

struct ResizingView: View {
public static let needsNewSize = Notification.Name("needsNewSize")

@State var resizing = false
var body: some View {
VStack {
Button(action: {
self.resizing.toggle()
NotificationCenter.default.post(name: Self.needsNewSize, object:
CGSize(width: self.resizing ? 800 : 400, height: self.resizing ? 350 : 200))
}, label: { Text("Resize") } )
}
.frame(minWidth: 400, maxWidth: .infinity, minHeight: 200, maxHeight: .infinity)
}
}

2) 窗口的所有者(在本例中为 AppDelegate )
import Cocoa
import SwiftUI
import Combine

@NSApplicationMain
class AppDelegate: NSObject, NSApplicationDelegate {

var window: NSWindow!
var subscribers = Set<AnyCancellable>()

func applicationDidFinishLaunching(_ aNotification: Notification) {

let contentView = ResizingView()

window = NSWindow(
contentRect: NSRect(x: 0, y: 0, width: 480, height: 300), // just default
styleMask: [.titled, .closable, .miniaturizable, .resizable, .fullSizeContentView],
backing: .buffered, defer: false)
window.center()
window.setFrameAutosaveName("Main Window")
window.contentView = NSHostingView(rootView: contentView)
window.makeKeyAndOrderFront(nil)

NotificationCenter.default.publisher(for: ResizingView.needsNewSize)
.sink(receiveCompletion: {_ in}) { [unowned self] notificaiton in
if let size = notificaiton.object as? CGSize {
var frame = self.window.frame
let old = self.window.contentRect(forFrameRect: frame).size

let dX = size.width - old.width
let dY = size.height - old.height

frame.origin.y -= dY // origin in flipped coordinates
frame.size.width += dX
frame.size.height += dY
self.window.setFrame(frame, display: true, animate: true)
}
}
.store(in: &subscribers)
}
...

关于macos - 在 SwiftUI macOS 应用程序中根据 NavigationView 更改窗口大小,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59385446/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com