由于List目前看起来不像是可配置的删除行分隔符,所以我使用ScrollViewVStack来创建文本元素的垂直布局.下面的例子:

ScrollView {
    VStack {
        // ...
        Text("Lorem ipsum dolor sit amet, consectetur adipiscing elit. Integer mattis ullamcorper tortor, nec finibus sapien imperdiet non. Duis tristique eros eget ex consectetur laoreet.")
            .lineLimit(0)
    }.frame(width: UIScreen.main.bounds.width)
}

生成的Text条渲染线被截断为一行.在ScrollView之外,它渲染为多行.除了明确设置Text帧的高度,我如何在ScrollView中实现这一点?

推荐答案

Xcode 11 GM年:

For any 100 view in a stack nested in a scrollview,使用.fixedSize(horizontal: false, vertical: true)解决方案:

ScrollView {
    VStack {
        Text(someString)
            .fixedSize(horizontal: false, vertical: true)
    }
}

This also works if there are multiple multiline texts:

ScrollView {
    VStack {
        Text(someString)
            .fixedSize(horizontal: false, vertical: true)
        Text(anotherLongString)
            .fixedSize(horizontal: false, vertical: true)
    }
}

If the contents of your stack are dynamic, the same solution works:

ScrollView {
    VStack {
        // Place a single empty / "" at the top of your stack.
        // It will consume no vertical space.
        Text("")
            .fixedSize(horizontal: false, vertical: true)

        ForEach(someArray) { someString in
            Text(someString)
              .fixedSize(horizontal: false, vertical: true)
        }
    }
}

Swift相关问答推荐

数据不会被ARC删除在swift'

依赖于@Environment的init@StateObject

它是RxSwift中直接用于可扩展视图的有效的可扩展BehaviorRelay值?

在数组中查找最大y值的x值

为什么这个快速代码不显示文本字段?

为什么我不能在这个 Swift 间接枚举中返回 self ?

如何判断设备是否为 Vision Pro - Xcode 15 Beta 4

带有可选字符串作为键路径的 SwiftUI iOS16 表

可以使 Swift init 仅可用于 Objective C 吗?

自定义碰撞形状出现在错误的位置

如何将变量添加到不属于可解码部分的 struct ?

如何在 SceneKit 上制作毛玻璃效果

Pod lib lint 命令找不到 watchos 模拟器

如何避免从模块导入函数

在 macOS (Swift) 上获取 BSD 驱动器名称的最佳方法是什么?

响应 UITextField (UIViewRepresentable) 中的特定按键

Xcode:方法参数的代码完成

SwiftUI 文本被意外截断

如何在 SwiftUI 中为删除和禁用配置 ContextMenu 按钮?

Xcode6 中的 Swift 类与 Cocoa Touch 类