【问题标题】:SwiftUI Align one text centre and one trailingSwiftUI 一个文本中心和一个尾随对齐
【发布时间】:2021-04-28 13:49:54
【问题描述】:

我正在尝试向我制作的自定义图表视图添加一些细节。我目前在 VStack 中有图表和标题。这是完美的工作,意味着标题在图表上方居中。

我现在想在标题下方和视图尾端的图表上方添加一个小文本标题,如下所示

我很难理解我需要使用哪些修饰符来正确设置对齐以实现此布局。任何帮助表示赞赏。

代码:

VStack {
    // Title Text
    Text(title)
        .font(.headline)
    
    // The new text I want added (text 2 in the image)  
    Text(min)
        .font(.caption)
        // My current attempt but I am not sure this is right
        .alignmentGuide(.trailing, computeValue: { _ in 0 })
        
    // Chart
    ChartView()
}

【问题讨论】:

    标签: ios swift swiftui


    【解决方案1】:

    只需将您的第二个 Text 包装在带有垫片的 HStack 中。此间隔将强制Text 向右移动。

    struct ContentView: View {
        var body: some View {
            VStack {
                // Title Text
                Text("Title")
                    .font(.headline)
                
                HStack {
                    Spacer() /// forces the Text to the right
                    
                    // The new text I want added (text 2 in the image)
                    Text("Text 2")
                        .font(.caption)
                }
                
                // Chart
                Rectangle()
                    .fill(Color.blue)
            }
        }
    }
    

    结果:

    【讨论】:

    • 非常感谢,我在用 SwiftUI 设计 UI 时需要习惯这种新的思维方式
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2020-05-23
    • 2021-06-16
    • 1970-01-01
    • 2012-02-08
    • 2011-10-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多