ios SwiftUI错误,在左侧显示多个行值,并破坏堆栈右侧的对齐

euoag5mw  于 2022-12-30  发布在  iOS
关注(0)|答案(3)|浏览(111)

当文本输入为多行时,我遇到了对齐问题。在本例中,第一部分显示了图像中正确显示的左侧和右侧文本。第二部分显示了3到4行文本,干扰了右侧对齐
我想让左边在多行中独立地调整大小。右边保持原样(2nd title2nd Value之间没有间隙)。
我的视图代码:

struct ContentView: View {
    var body: some View {
        
        Form {

            Section {
                Text("single line")
            }
            
            TwoLineView(firstTitle: "1st Title", fistValue: "Value",
                           secondTitle: "2nd Title", secondValue: "2nd Value")
            
            TwoLineView(firstTitle: "1st Title", fistValue: "test",
                           secondTitle: "2nd Title", secondValue: "2nd Value")
            
            Section {
                Text("mutiple lines")
            }
            
            TwoLineView(firstTitle: "1st Title", fistValue: "This is long. This is long value. This is long value",
                           secondTitle: "2nd Title", secondValue: "2nd Value")
            
            TwoLineView(firstTitle: "1st Title", fistValue: "test",
                           secondTitle: "2nd Title", secondValue: "2nd Value")
        }
        
    }
    
    
    
    
    struct ContentView_Previews: PreviewProvider {
        static var previews: some View {
            ContentView()
        }
    }
}

struct TwoLineView: View{
    var firstTitle: String
    var fistValue: String
    var secondTitle: String
    var secondValue: String
        
    var body: some View {
        HStack(alignment: VerticalAlignment.center, spacing: 0) {
            VStack(alignment: HorizontalAlignment.leading, spacing: 0) {
                Text(firstTitle).lineLimit(1)
                    .font(.system(size: 30, weight: .heavy, design: .default))
                
                Text(fistValue)
                    .frame(maxHeight: .infinity)
            }
            Spacer()
            VStack(alignment: HorizontalAlignment.leading, spacing: 0) {
                Text(secondTitle).lineLimit(1)
                    .font(.system(size: 30, weight: .heavy, design: .default))
                
                Text(secondValue).lineLimit(1)
                
                Spacer()
                
            }
            Spacer(minLength: 45)
        }
    }
}

预期:单行或多行值。我不希望在标题和值之间显示间隙。(图像中的第二部分在2nd title2nd Value之间有间隙,这是糟糕的UX体验)
问题屏幕截图:-x1c 0d1x

ldioqlga

ldioqlga1#

只需删除frame,然后在VStack中添加spacer

HStack(alignment: VerticalAlignment.center, spacing: 0) {
                    VStack(alignment: HorizontalAlignment.leading, spacing: 0) {
                        Text("1st Title ").lineLimit(1)
                            .font(.system(size: 30, weight: .heavy, design: .default))
                        
                        Text("This is long value. This is long value. This is long value")
                        
                            .frame(maxHeight: .infinity)
                    }
                    Spacer()
                    VStack(alignment: HorizontalAlignment.leading, spacing: 0) {
                        Text("2nd title").lineLimit(1)
                            .font(.system(size: 30, weight: .heavy, design: .default))
                        
                        Text("2nd Value").lineLimit(1)
                        Spacer() //here
                    //.frame(maxHeight: .infinity)
                    }
                    Spacer(minLength: 45)
                }
zzoitvuj

zzoitvuj2#

struct ContentView: View {
    var body: some View {
        Form {
            Section {
                Text("single line")
            }
            TwoLineView(firstTitle: "1st Title", fistValue: "Value",
                        secondTitle: "2nd Title", secondValue: "2nd Value")
            TwoLineView(firstTitle: "1st Title", fistValue: "Test",
                        secondTitle: "2nd Title", secondValue: "2nd Value")
            Section {
                Text("mutiple lines")
            }
            TwoLineView(firstTitle: "1st Title", fistValue: "This is long. This is long value. This is long value",
                        secondTitle: "2nd Title", secondValue: "2nd Value")
            TwoLineView(firstTitle: "1st Title", fistValue: "Test",
                        secondTitle: "2nd Title", secondValue: "2nd Value")
        }
    }
}
struct TwoLineView: View{
    var firstTitle: String
    var fistValue: String
    var secondTitle: String
    var secondValue: String
    
    var body: some View {
        HStack(alignment: VerticalAlignment.center, spacing: 0) {
            VStack(alignment: HorizontalAlignment.leading, spacing: 0) {
                Text(firstTitle).lineLimit(1)
                    .font(.system(size: 30, weight: .heavy, design: .default))
                
                Text(fistValue)
                Spacer() // add
            }
            Spacer()
            VStack(alignment: HorizontalAlignment.leading, spacing: 0) {
                Text(secondTitle).lineLimit(1)
                    .font(.system(size: 30, weight: .heavy, design: .default))
                
                Text(secondValue).lineLimit(1)
                Spacer()
            }
            Spacer(minLength: 45)
        }
    }
}
struct ContentView_Previews: PreviewProvider {
    static var previews: some View {
        ContentView()
    }
}
lh80um4z

lh80um4z3#

只需移除框架并在VStack中添加垫片。
这解决了问题,并显示我所需要的。但是,一个问题是它打破了右下角框中的对齐
[![在此输入图像说明][1]][1]

相关问题