【问题标题】:Display Text in left of view在视图左侧显示文本
【发布时间】:2021-10-24 16:18:46
【问题描述】:

我的应用程序上有一个屏幕,女巫从 firebase 获取数据然后显示它。但我想让 2 Text() 查看另一个下方的文本,但由于某种原因,文本没有显示在视图的左侧

问题:

我的代码:

struct ResourceItem: View {
    
    var title: String
    var url: String
    
    var body: some View {
        VStack {
            
            Link(destination: URL(string: url)!) {
                
                Text(title)
                    .bold()
                    .font(.system(size: 18))
                
                Text(url)
                    .bold()
                    .foregroundColor(.gray)
                    
            }
        }
    }
}

【问题讨论】:

标签: ios swift swiftui


【解决方案1】:

Link 使用默认配置创建它自己的VStack。这就是为什么当您将 Link 放在 VStack 中时 - 外部容器会被忽略。

你需要把VStack(alignment: .leading)放在Link里面:

struct ResourceItem: View {
    
    var title: String
    var url: String
    
    var body: some View {
        Link(destination: URL(string: url)!) {
            VStack(alignment: .leading) {
                Text(title)
                    .bold()
                    .font(.system(size: 18))
                Text(url)
                    .bold()
                    .foregroundColor(.gray)
            }
        }
    }
}

【讨论】:

    【解决方案2】:

    如果您需要将这些文本放在一行 - 只需将它们放在水平堆栈中 -

    HStack (alignment: .leading) {
    Text(title)
        .bold()
        .font(.system(size: 18))
    Text(url)
        .bold()
        .foregroundColor(.gray)
    

    }

    【讨论】:

    • 这不是他们想要的。他们在谈论文本的对齐方式。 "i want to have 2 Text() view the one below the other"
    【解决方案3】:

    试试这个:

    VStack (alignment: .leading) {
        Text(title)
            .bold()
            .font(.system(size: 18))
        Text(url)
            .bold()
            .foregroundColor(.gray)
    }
    

    【讨论】:

    • 好的,把这个(alignment: .leading)添加到第一个VStack。
    • 不是VStack,是Link()
    • 我知道这是一个链接,请将我的答案放在链接中。你把它放在哪里,在链接之外?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-05-29
    • 1970-01-01
    • 1970-01-01
    • 2022-01-19
    • 1970-01-01
    相关资源
    最近更新 更多