Jul*_*ert 6 layout ios swift swiftui swiftui-list
SwiftUI 是否有可能在一行中仅显示尽可能多的(固定大小)视图而不进行裁剪,例如剪切视图的某些部分。
\n ----------------------------------------- \n| |\n| ------- ------- ------- ------- -------\n| | VIEW1 | | VIEW2 | | VIEW3 | | VIEW4 | | VIEW5 |\n| ------- ------- ------- ------- -------\n| |\n -----------------------------------------\nRun Code Online (Sandbox Code Playgroud)\n在此示例中VIEW5应隐藏,因为它相对于父视图完全超出范围。\n这可以通过.clipped().
VIEW4也应该完全隐藏!\n因为如果要显示它,则必须将其切断。\n所有其他视图都应正常渲染。
最初的尝试存在以下问题:
\nitemView比外部 的宽度大得多VStack,因此前缘未对齐 => \xe2\x98\x91\xef\xb8\x8f解决方案 ----------------------------------------- \n| |\n| ------- ------- ------- ------- -------\n| | VIEW1 | | VIEW2 | | VIEW3 | | VIEW4 | | VIEW5 |\n| ------- ------- ------- ------- -------\n| |\n -----------------------------------------\nRun Code Online (Sandbox Code Playgroud)\n\n
使用在框架上对齐的剪辑HStack(使用 Xcode 12.1 / iOS 14.1 测试)
private var itemView: some View {
HStack {
ForEach(items, id: \.self) { item in
Text(item)
.lineLimit(1)
.fixedSize()
.padding(.all, 5)
.font(.body)
.background(Color.blue)
.foregroundColor(Color.white)
.cornerRadius(5)
}
}
.frame(maxWidth: 350, alignment: .leading)
.clipped()
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
3991 次 |
| 最近记录: |