我对SwiftUI有个问题。当我在另一个VStack中使用多个VStack时,文本截断有一些奇怪的问题。
下面是一个最小的可重复的例子:

"B“标签被截断,但不应该被截断。如果我更改"A“文本或"B”文本,通过添加或删除一些字母,"B“标签有时是错误的,有时是可以的。

以下是相应的代码:
struct ContentView: View {
var body: some View {
VStack {
VStack {
Text("AAAAAAAAAAAAAAAAAAAA")
}.background(Color.yellow)
VStack {
Text("BBBBBBBB")
Text("CCC")
}.background(Color.orange)
}.background(Color.blue)
}
}我是错过了什么,还是这是SwiftUI的一个bug?
谢谢!
环境: Xcode 11.4.1,iOS 13.4.1,在预览、模拟器和设备上测试
编辑:根据您正在使用的预览/模拟器设备,这会有不同的行为。例如,上面的代码,复制粘贴到一个新的项目,是错误的iPhone 11 Pro / 11 Pro Max和8+。在iPhone 8/ 11 / SE 1&2上,这很好。那么,它可能与SwiftUI布局引擎中的一些计算/舍入问题有关吗?
发布于 2020-04-22 13:07:53
.fixedSize(horizontal: true, vertical: false)应该修复它。
struct ContentView: View {
var body: some View {
VStack {
VStack {
Text("AAAAAAAAAAAAAAAAAAAA")
.fixedSize(horizontal: true, vertical: false)
}.background(Color.yellow)
VStack {
Text("BBBBBBBB")
.fixedSize(horizontal: true, vertical: false)
Text("CCC")
.fixedSize(horizontal: true, vertical: false)
}.background(Color.orange)
}.background(Color.blue)
}
}适用于多行文本:.fixedSize(horizontal: false, vertical: true)
struct ContentView2: View {
var body: some View {
VStack {
VStack {
Text("Swift enums are really powerful, but they can often be made even more capable when mixed with")
.fixedSize(horizontal: false, vertical: true)
.multilineTextAlignment(.center)
}.background(Color.yellow)
VStack {
Text("Swift enums are really powerful, but they can often be made even more capable when mixed ")
.fixedSize(horizontal: false, vertical: true)
.multilineTextAlignment(.center)
Text("Swift enums are really powerful, but they can often be made even more capable ")
.fixedSize(horizontal: false, vertical: true)
.multilineTextAlignment(.center)
}.background(Color.orange)
}.background(Color.blue)
}
}
struct ContentView2_Previews: PreviewProvider {
static var previews: some View {
ContentView2()
}
}发布于 2020-06-23 20:48:47
FYI:似乎是固定在SwiftUI 2 :)
https://stackoverflow.com/questions/61363653
复制相似问题