Tim*_*Tim 46 ios swift swiftui
我有一个 HStack:
struct BottomList: View {
var body: some View {
HStack() {
ForEach(navData) { item in
NavItem(image: item.icon, title: item.title)
}
}
}
}
Run Code Online (Sandbox Code Playgroud)
如何以相等的间距将其内容完美地居中并自动填充整个宽度?
仅供参考,就像 Bootstraps CSS 类 .justify-content-around
sva*_*all 106
可以使用frame带有.infinityformaxWidth参数的布局修饰符来实现这一点,而无需额外的ShapeView。
struct ContentView: View {
var data = ["View", "V", "View Long"]
var body: some View {
VStack {
// This will be as small as possible to fit the data
HStack {
ForEach(data, id: \.self) { item in
Text(item)
.border(Color.red)
}
}
// The frame modifier allows the view to expand horizontally
HStack {
ForEach(data, id: \.self) { item in
Text(item)
.frame(maxWidth: .infinity)
.border(Color.red)
}
}
}
}
}
Run Code Online (Sandbox Code Playgroud)

kga*_*dis 27
我Spacer()在每个项目之后插入...但对于最后一个项目,不要添加Spacer():
struct BottomList: View {
var body: some View {
HStack() {
ForEach(data) { item in
Item(title: item.title)
if item != data.last { // match everything but the last
Spacer()
}
}
}
}
}
Run Code Online (Sandbox Code Playgroud)
(注意:接受的答案.frame(maxWidth: .infinity)并不适用于所有情况:当涉及到具有不同宽度的项目时,它对我不起作用)
Joh*_* M. 15
各个*Stack类型将尽量缩到最小尺寸可以容纳他们的孩子的意见。如果子视图具有理想的大小,则*Stack不会扩展以填满屏幕。这可以通过将每个孩子放在 aRectangle中的空白之上来克服ZStack,因为 aShape会尽可能地扩大。一个方便的方法是通过扩展View:
extension View {
func inExpandingRectangle() -> some View {
ZStack {
Rectangle()
.fill(Color.clear)
self
}
}
}
Run Code Online (Sandbox Code Playgroud)
然后你可以这样称呼它:
struct ContentView: View {
var data = ["View", "View", "View"]
var body: some View {
VStack {
// This will be as small as possible to fit the items
HStack {
ForEach(data, id: \.self) { item in
Text(item)
.border(Color.red)
}
}
// Each item's invisible Rectangle forces it to expand
// The .fixedSize modifier prevents expansion in the vertical direction
HStack {
ForEach(data, id: \.self) { item in
Text(item)
.inExpandingRectangle()
.fixedSize(horizontal: false, vertical: true)
.border(Color.red)
}
}
}
}
}
Run Code Online (Sandbox Code Playgroud)
您可以HStack根据需要调整间距。
Moj*_*ini 13
如果项目是全角兼容的,它将自动完成,您可以将项目包裹在间隔符之间以实现它:
struct Resizable: View {
let text: String
var body: some View {
HStack {
Spacer()
Text(text)
Spacer()
}
}
}
Run Code Online (Sandbox Code Playgroud)

那么你。可以在循环中使用它,例如:
HStack {
ForEach(data, id: \.self) { item in
Resizable(text: item)
}
}
Run Code Online (Sandbox Code Playgroud)
您还可以spacing在堆栈中使用...即
HStack(spacing: 30){
Image("NetflixLogo")
.resizable()
.scaledToFit()
.frame(width: 40)
Text("TV Show")
Text("Movies")
Text("My List")
}
.frame(maxWidth: .infinity)
Run Code Online (Sandbox Code Playgroud)
输出结果看起来像这样...
如果数组有重复值,请使用 array.indices 省略最后一个元素后面的间隔符。
HStack() {
ForEach(data.indices) { i in
Text("\(data[i])")
if i != data.last {
Spacer()
}
}
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
19986 次 |
| 最近记录: |