英文:
SwiftUI .sheet view resizing issue (testing on macos)
问题
我有以下示例代码:
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("一些文本")
Text("一些文本")
Text("一些文本")
Text("一些文本")
Text("一些文本")
}
}
}.padding()
}
.toolbar {
Button {
showModal = true
} label: {
Image(systemName: "plus")
}
}
}
}
当弹出表单时,大小是正确的。
当我切换按钮时,大小会根据消失的UI元素正确重新计算。
当我再次切换时,尺寸不正确,似乎没有考虑到新显示的项目。如何使表单重新调整大小?
英文:
I have the following sample code:
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.
When I toggle the button, the size correctly recalculates based on the UI elements that disappeared.
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?
答案1
得分: 0
Sure, here's the translated content:
希望这能帮助某人。在经过几小时的搜索后,我找到了这篇看似无关的帖子:如何在SwiftUI中获取文本宽度?
就像在所引用的帖子的被接受答案中一样,我向我的VStack
添加了.fixedSize()
,这解决了弹出视图大小调整的问题。
if 条件 {
VStack {
Text("一些文本")
Text("一些文本")
Text("一些文本")
Text("一些文本")
Text("一些文本")
}
.fixedSize()
}
英文:
Well, hopefully this helps someone. After hours of search, i've found this seemingly unrelated post: How to get Text width with SwiftUI?
Just like in the accepted answer of the referred post, I've added .fixedSize()
to my VStack
which has fixed the sheet's resizing issue.
if hmm {
VStack {
Text("Some Text")
Text("Some Text")
Text("Some Text")
Text("Some Text")
Text("Some Text")
}
.fixedSize()
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论