SwiftUI .sheet 视图调整大小问题(在 Macos 上测试)

2023-12-01

我有以下示例代码:

import SwiftUI

struct ContentView: View {
    @State private var showModal = false
    @State private var hmm = true
    
    var body: some View {
        VStack {
            Image(systemName: "globe")
                .imageScale(.large)
                .foregroundColor(.accentColor)
            Text("Hello, world!")
        }
        .padding()
        .sheet(isPresented: $showModal) {
            VStack {
                Button {
                    hmm.toggle()
                } label: {
                    Image(systemName: "plus")
                }
                if hmm {
                    VStack {
                        Text("Some Text")
                        Text("Some Text")
                        Text("Some Text")
                        Text("Some Text")
                        Text("Some Text")
                    }
                }
            }.padding()
        }
        .toolbar {
            Button {
                showModal = true
            } label: {
                Image(systemName: "plus")
            }
        }
    }
}

When the sheet appears, the size is correct. enter image description here When I toggle the button, the size correctly recalculates based on the UI elements that disappeared. enter image description here When I toggle again, the sizing is incorrect and does not seem to account for the newly visible items. How can I get the sheet to resize properly? enter image description here


好吧,希望这对某人有帮助。经过几个小时的搜索,我发现了这个看似无关的帖子:如何使用 SwiftUI 获取文本宽度?

就像在提到的帖子的接受答案中一样,我添加了.fixedSize() to my VStack这解决了工作表的调整大小问题。

if hmm {
    VStack {
        Text("Some Text")
        Text("Some Text")
        Text("Some Text")
        Text("Some Text")
        Text("Some Text")
    }
    .fixedSize()
}
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

SwiftUI .sheet 视图调整大小问题(在 Macos 上测试) 的相关文章

随机推荐