AskOverflow.Dev

AskOverflow.Dev Logo AskOverflow.Dev Logo

AskOverflow.Dev Navigation

  • 主页
  • 系统&网络
  • Ubuntu
  • Unix
  • DBA
  • Computer
  • Coding
  • LangChain

Mobile menu

Close
  • 主页
  • 系统&网络
    • 最新
    • 热门
    • 标签
  • Ubuntu
    • 最新
    • 热门
    • 标签
  • Unix
    • 最新
    • 标签
  • DBA
    • 最新
    • 标签
  • Computer
    • 最新
    • 标签
  • Coding
    • 最新
    • 标签
主页 / coding / 问题 / 79555645
Accepted
Marcel Derks
Marcel Derks
Asked: 2025-04-04 22:53:45 +0800 CST2025-04-04 22:53:45 +0800 CST 2025-04-04 22:53:45 +0800 CST

如何在 SwiftUI 中右对齐固有大小的 VStack 中的内容

  • 772

这可能是一个简单的问题,但我一直在努力。

假设我在 SwiftUI 中有以下视图布局

我希望日期字符串10.5.2020在父 VStack 中右对齐。

但重要的是,这样做不会破坏 VStack 的固有大小。

我之所以这么说,是因为最明显的两个解决方案都是这样做的。

  1. 用 围起来,并在左边HStack放一个。Spacer
  2. 使用框架maxWidth: .infinity和alignment: .trailing

我对对齐指南进行了更深入的研究,但无法解决这个问题。

到目前为止我的最佳结果是GeometryReader,将文本标签的宽度设置为几乎与周围 VStack 的宽度相同。

但不幸的是,我的目标结构要复杂得多,这种方法会导致该结构出现问题。而且GeometryReader似乎不适合进行对齐。

struct ContentView: View {
    var body: some View {
        ScrollView {
            VStack(alignment: .leading) {
                
                HStack {
                    VStack(alignment: .leading) {
                        Text("Alignment Test")
                        Text("This is a longer text left aligned")
                         
                        
                        Text("10.5.2020")
                    }
                    .padding(16)
                    .background(Color.blue)
                    
                    Spacer(minLength: 50)
                }
                
                
                HStack {
                    VStack(alignment: .leading) {
                        Text("left")
                        Text("Date should rigzht align to here <-")
                        Text("10.5.2020")
                    }
                    .padding(16)
                    .background(Color.blue)
                    
                    Spacer(minLength: 50)
                }
            }
            .frame(maxWidth: .infinity, maxHeight: .infinity)
            .border(.red)
        }
        .frame(maxWidth: .infinity, maxHeight: .infinity)
        .background(Color.gray.opacity(0.5))
    }
}

预览:

在此处输入图片描述

swiftui
  • 2 2 个回答
  • 41 Views

2 个回答

  • Voted
  1. Best Answer
    Benzy Neez
    2025-04-05T00:04:39+08:002025-04-05T00:04:39+08:00

    .frame可以使用和.fixedSize修饰符的组合来实现对齐。

    1. 应用于.frame(maxWidth: .infinity, alignment: .trailing)您想要右对齐的文本。这会导致文本扩展到可用的最大宽度,内容向右对齐。

    通常情况下,这也会导致容器扩展到整个可用宽度。因此,为了防止这种情况发生:

    1. 应用于.fixedWidth()。VStack这告诉它采用其内容的理想宽度。

    因此,它的工作方式是,VStack将大小调整为其内容的理想宽度,然后,在这个限制范围内,maxWidth: .infinity文本上的就会生效。

    ScrollView {
        VStack(alignment: .leading) {
    
            HStack {
                VStack(alignment: .leading) {
                    Text("Alignment Test")
                    Text("This is a longer text left aligned")
                    Text("10.5.2020")
                        .frame(maxWidth: .infinity, alignment: .trailing) // 👈 added
                }
                .padding(16)
                .fixedSize() // 👈 added
                .background(Color.blue)
    
                Spacer(minLength: 50)
            }
    
            HStack {
                VStack(alignment: .leading) {
                    Text("left")
                    Text("Date should right align to here ->")
                    Text("10.5.2020")
                        .frame(maxWidth: .infinity, alignment: .trailing) // 👈 added
                }
                .padding(16)
                .fixedSize() // 👈 added
                .background(Color.blue)
    
                Spacer(minLength: 50)
            }
        }
        .frame(maxWidth: .infinity, maxHeight: .infinity)
        .border(.red)
    }
    .frame(maxWidth: .infinity, maxHeight: .infinity)
    .background(Color.gray.opacity(0.5))
    

    截屏


    编辑在评论中,您说较长的文本可能太长,需要换行。但应用.fixedSize()将阻止换行。

    作为替代方法,您可以考虑使用 1 列Grid:

    • 将内部替换VStack为Grid。
    • 添加.gridCellAnchor(.trailing)日期。
    ScrollView {
        VStack(alignment: .leading) {
    
            HStack {
                Grid(alignment: .leading) { // 👈 VStack replaced
                    Text("Alignment Test")
                    Text("This is a longer text left aligned")
                    Text("10.5.2020")
                        .gridCellAnchor(.trailing) // 👈 added
                }
                .padding(16)
                .background(Color.blue)
    
                Spacer(minLength: 50)
            }
    
            HStack {
                Grid(alignment: .leading) {
                    Text("left")
                    Text("The quick brown fox jumps over the lazy dog")
                    Text("10.5.2020")
                        .gridCellAnchor(.trailing)
                }
                .padding(16)
                .background(Color.blue)
    
                Spacer(minLength: 50)
            }
        }
        .frame(maxWidth: .infinity, maxHeight: .infinity)
        .border(.red)
    }
    .frame(maxWidth: .infinity, maxHeight: .infinity)
    .background(Color.gray.opacity(0.5))
    

    截屏

    • 1
  2. Marcel Derks
    2025-04-05T14:14:38+08:002025-04-05T14:14:38+08:00

    我找到了一种替代解决方案,它不像使用 那样优雅Grid,但它仍然可以工作,我想在这里记录它。例如,Grid 在 iOS 16 之前不可用,您可能需要在生产中支持这些设备。

     ScrollView {
                VStack(alignment: .leading) {
                    
                    HStack {
                        VStack(alignment: .leading) {
                            Text("Title")
                            Text("Not AI")
                            Text("10.5.2020").foregroundStyle(.clear)
                        }
                        .padding(16)
                        .background(Color.blue)
                        .overlay(alignment: .bottomTrailing) {
                            Text("10.5.2020")
                                .padding(EdgeInsets(top: 0, leading: 0, bottom: 16, trailing: 16))
                        }
                        
                        Spacer(minLength: 50)
                    }
                    
                    HStack {
                        VStack(alignment: .leading) {
                            Text("left")
                            Text("The quick brown fox jumps over the lazy dog>")
                            Text("10.5.2020").foregroundStyle(.clear)
                        }
                        .padding(16)
                        .background(Color.blue)
                        .overlay(alignment: .bottomTrailing) {
                            Text("10.5.2020")
                                .padding(EdgeInsets(top: 0, leading: 0, bottom: 16, trailing: 16))
                        }
                        
                        Spacer(minLength: 50)
                    }
                }
                .frame(maxWidth: .infinity, maxHeight: .infinity)
                .border(.red)
            }
            .frame(maxWidth: .infinity, maxHeight: .infinity)
            .background(Color.gray.opacity(0.5))
    

    在这种方法中,我使用bottomTrailing对齐的覆盖来添加日期文本。

    请注意,我保留了原始日期文本VStack以占据那里的空间。foregroundStyle = .clear 我隐藏了该间隔文本,因此它不会显示在 UI 中。

    这种方法的缺点是,它只有当您想在右下角添加标签时才有效。

    在此处输入图片描述

    • 0

相关问题

  • SwiftUI:如何创建颜色较浅的渐变?

  • SwiftUI - 带有“绑定”变量的通用视图

  • 创建日历日视图

  • 如何让 SwiftUI 的动画变慢

  • 不理解 SwiftUI 文本布局

Sidebar

Stats

  • 问题 205573
  • 回答 270741
  • 最佳答案 135370
  • 用户 68524
  • 热门
  • 回答
  • Marko Smith

    重新格式化数字,在固定位置插入分隔符

    • 6 个回答
  • Marko Smith

    为什么 C++20 概念会导致循环约束错误,而老式的 SFINAE 不会?

    • 2 个回答
  • Marko Smith

    VScode 自动卸载扩展的问题(Material 主题)

    • 2 个回答
  • Marko Smith

    Vue 3:创建时出错“预期标识符但发现‘导入’”[重复]

    • 1 个回答
  • Marko Smith

    具有指定基础类型但没有枚举器的“枚举类”的用途是什么?

    • 1 个回答
  • Marko Smith

    如何修复未手动导入的模块的 MODULE_NOT_FOUND 错误?

    • 6 个回答
  • Marko Smith

    `(表达式,左值) = 右值` 在 C 或 C++ 中是有效的赋值吗?为什么有些编译器会接受/拒绝它?

    • 3 个回答
  • Marko Smith

    在 C++ 中,一个不执行任何操作的空程序需要 204KB 的堆,但在 C 中则不需要

    • 1 个回答
  • Marko Smith

    PowerBI 目前与 BigQuery 不兼容:Simba 驱动程序与 Windows 更新有关

    • 2 个回答
  • Marko Smith

    AdMob:MobileAds.initialize() - 对于某些设备,“java.lang.Integer 无法转换为 java.lang.String”

    • 1 个回答
  • Martin Hope
    Fantastic Mr Fox msvc std::vector 实现中仅不接受可复制类型 2025-04-23 06:40:49 +0800 CST
  • Martin Hope
    Howard Hinnant 使用 chrono 查找下一个工作日 2025-04-21 08:30:25 +0800 CST
  • Martin Hope
    Fedor 构造函数的成员初始化程序可以包含另一个成员的初始化吗? 2025-04-15 01:01:44 +0800 CST
  • Martin Hope
    Petr Filipský 为什么 C++20 概念会导致循环约束错误,而老式的 SFINAE 不会? 2025-03-23 21:39:40 +0800 CST
  • Martin Hope
    Catskul C++20 是否进行了更改,允许从已知绑定数组“type(&)[N]”转换为未知绑定数组“type(&)[]”? 2025-03-04 06:57:53 +0800 CST
  • Martin Hope
    Stefan Pochmann 为什么 {2,3,10} 和 {x,3,10} (x=2) 的顺序不同? 2025-01-13 23:24:07 +0800 CST
  • Martin Hope
    Chad Feller 在 5.2 版中,bash 条件语句中的 [[ .. ]] 中的分号现在是可选的吗? 2024-10-21 05:50:33 +0800 CST
  • Martin Hope
    Wrench 为什么双破折号 (--) 会导致此 MariaDB 子句评估为 true? 2024-05-05 13:37:20 +0800 CST
  • Martin Hope
    Waket Zheng 为什么 `dict(id=1, **{'id': 2})` 有时会引发 `KeyError: 'id'` 而不是 TypeError? 2024-05-04 14:19:19 +0800 CST
  • Martin Hope
    user924 AdMob:MobileAds.initialize() - 对于某些设备,“java.lang.Integer 无法转换为 java.lang.String” 2024-03-20 03:12:31 +0800 CST

热门标签

python javascript c++ c# java typescript sql reactjs html

Explore

  • 主页
  • 问题
    • 最新
    • 热门
  • 标签
  • 帮助

Footer

AskOverflow.Dev

关于我们

  • 关于我们
  • 联系我们

Legal Stuff

  • Privacy Policy

Language

  • Pt
  • Server
  • Unix

© 2023 AskOverflow.DEV All Rights Reserve