简体   繁体   English

SwiftUI - HStack 中有两个文本的奇怪间距

[英]SwiftUI - weird spacing in HStack with two Texts

I am creating a "News" SwiftUI widget and I need to display rows, each containing time and news header.我正在创建一个“新闻”SwiftUI 小部件,我需要显示行,每行都包含时间和新闻 header。

I am using a VStack containing HStack 's for each item but I am encountering an issue with extra HStack spacing between the two Text 's.我正在为每个项目使用包含VStackHStack ,但我遇到了两个Text之间额外的 HStack 间距的问题。 I need the left Text taking all the width needed for its content and the right Text taking all the width that is left.我需要左侧Text占用其内容所需的所有宽度,右侧Text占用所有左侧宽度。 Seems to work as expected for the longer date (second line) for not for the first one.对于较长的日期(第二行),似乎可以按预期工作,而不是第一个。

In AutoLayout world I would need to play around with Content hugging / Compression resistance priority to achieve what I need but not sure what to do in SwiftUI.在 AutoLayout 世界中,我需要使用内容拥抱/抗压缩优先级来实现我需要的东西,但不确定在 SwiftUI 中该怎么做。

Here's my code for creating the view:这是我创建视图的代码:

  private func createView(from headers: [NewsHeaderEntry.NewsHeader], with first: Int) -> some View {
        GeometryReader { metrics in
            VStack(alignment: .leading, spacing: 16) {
                title
                ForEach(headers[0...first - 1]) { header in
                    HStack(spacing: 4) {
                            Text(header.newsAt)
                                .font(.system(size: 12))
                                .fontWeight(.light)
                                .background(.green)
                                .frame(alignment: .leading)
                            Text(header.title)
                                .font(.system(size: 14))
                                .fontWeight(.semibold)
                                .redacted(reason: header.isPlaceholder ? .placeholder : .init())
                                .background(.yellow)
                                .lineLimit(2)
                                .frame(maxWidth: .infinity, alignment: .trailing)
                        }
                        .background(.red)
                        .frame(maxWidth: .infinity, alignment: .center)
                    .padding(.horizontal, 8)
                }
                Spacer()
            }
            .frame(width: metrics.size.width)
            .background(.blue)
        }
    }

I have also tried setting maxWidth:.infinity for the left Text as well but it resulted in equal widths for both Text s which is not what I need.我也尝试为左侧Text设置maxWidth:.infinity ,但它导致两个Text的宽度相等,这不是我需要的。

结果:

Update : Without .padding(.horizontal, 8) the spacing works as expected but I need the horizontal spacing from the design perspective.更新:没有.padding(.horizontal, 8)间距按预期工作,但我需要从设计角度来看水平间距。

[ [没有填充的结果: 2 : 2

What you're looking for is the default behavior of the stack.您正在寻找的是堆栈的默认行为。 Your frame modifiers are the issue:您的框架修饰符是问题所在:

private func createView(from headers: [NewsHeaderEntry.NewsHeader], with first: Int) -> some View {
    GeometryReader { metrics in
        VStack(alignment: .leading, spacing: 16) {
            title
            ForEach(headers[0...first - 1]) { header in
                HStack(spacing: 4) {
                    Text(header.newsAt)
                        .font(.system(size: 12))
                        .fontWeight(.light)
                        .background(.green)
                    Text(header.title)
                        .font(.system(size: 14))
                        .fontWeight(.semibold)
                        .redacted(reason: header.isPlaceholder ? .placeholder : .init())
                        .background(.yellow)
                        .lineLimit(2)
                }
                .background(.red)
                .padding(.horizontal, 8)
            }
            Spacer()
        }
        .frame(width: metrics.size.width)
        .background(.blue)
    }
}

模拟器

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM