当 SwiftUI 中的文本为 1 行或 2 行时,如何在单元格之间显示相等的间距或高度

问题描述 投票:0回答:1

代码:如果有的话

favCell > Text(item.title ?? "") in 2 lines
那么在该行中显示不均匀的视图线为什么? 如何使第1行文字高度或顶部和底部之间的间距也等于2行文字请指导

我应该在哪里改变才能获得平等,请指导

enter image description here

@ViewBuilder func favView() -> some View {
VStack {
    
    LazyVGrid(columns: [
        GridItem(.flexible(), spacing: 20),
        GridItem(.flexible(), spacing: 20),
        GridItem(.flexible(), spacing: 20),
        GridItem(.flexible(), spacing: 20)
    ], spacing: 20) {
        
        ForEach(0..<favMenu.count + 1, id: \.self) { ind in
            if (ind == favMenu.count) {
                addMoreButton()
            } else {
                favoriteButton(for: ind)
            }
        }
    }
    .padding(.top, 16)
}
.padding(.top, 35)
.padding(.horizontal, 24)
}

@ViewBuilder func favoriteButton(for index: Int) -> some View {
Button {
    handleFavoriteSelection(at: index)
} label: {
    favCell(item: favMenu[index], index: index)
}
.buttonStyle(.plain)
}

@ViewBuilder func favCell(item: AllMenu, index: Int) -> some View {
VStack(spacing: 10) {
    VStack(spacing: 0) {
        ZStack {
            RoundedRectangle(cornerRadius: 15)
                .fill(Color.Neumorphic.main)
                .softOuterShadow()
                .frame(width: 65, height: 65)
            URLImageView(url: item.icon ?? "", width: 25, height: 25, renderingMode: .template, tintColor: .appGreen)
                .frame(width: 25, height: 25)
        }
        .frame(width: 65, height: 65)
    }
    
    Text(item.title ?? "")
        .font(.calibriRegular(with: 13))
        .foregroundColor(.black)
        .lineLimit(2)
        .multilineTextAlignment(.center)
        .frame(width: 70)
        .fixedSize(horizontal: true, vertical: false)
        .padding(.horizontal, 4)
        .padding(.bottom, 10)
    
}
}
swift swiftui text view lazyvgrid
1个回答
0
投票

您可能需要为每个

.alignment
添加
GridItem

GridItem(.flexible(), spacing: 20, alignment: .top) //<- here

输出:

enter image description here

© www.soinside.com 2019 - 2024. All rights reserved.