【问题标题】:How to change/define Text position如何更改/定义文本位置
【发布时间】:2019-11-25 01:20:43
【问题描述】:

如果我有这个代码:

import SwiftUI

struct Test: View {
    var body: some View {
        Text("Hello World!")
            .font(.title)
            .foregroundColor(.white)
            .padding()
            .background(Color.red)
    }
}

我有这个结果:

如何从视图上部的代码“Hello World”精确定位,如下面的截图所示?

【问题讨论】:

    标签: swiftui


    【解决方案1】:

    要将您的 Text 移动到屏幕顶部,您需要将其包裹在 VStack 中并在下方放置 Spacer 以将文本推送到屏幕顶部:

    var body: some View {
        VStack {
            Text("Hello World!")
                // all the modifiers
            Spacer()
        }
    }
    

    Text 只占用它需要的空间,而Spacer 将占用给它的任何空间。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-10-04
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多