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

huangapple go评论63阅读模式
英文:

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")
            }
        }
    }
}

当弹出表单时,大小是正确的。
SwiftUI .sheet视图大小调整问题(在macOS上进行测试)
当我切换按钮时,大小会根据消失的UI元素正确重新计算。
SwiftUI .sheet视图大小调整问题(在macOS上进行测试)
当我再次切换时,尺寸不正确,似乎没有考虑到新显示的项目。如何使表单重新调整大小?
SwiftUI .sheet视图大小调整问题(在macOS上进行测试)

英文:

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.
SwiftUI .sheet视图大小调整问题(在macOS上进行测试)
When I toggle the button, the size correctly recalculates based on the UI elements that disappeared.
SwiftUI .sheet视图大小调整问题(在macOS上进行测试)
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?
SwiftUI .sheet视图大小调整问题(在macOS上进行测试)

答案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()
}

huangapple
  • 本文由 发表于 2023年6月21日 23:46:38
  • 转载请务必保留本文链接:https://go.coder-hub.com/76525061.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定