当文本在 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

输出:

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