var body: some View {
VStack(alignment: .leading) {
Text("Title")
.bold()
.font(Font.custom("Helvetica Neue", size: 36.0))
...
Button(action: {}){
Text("Create")
.bold()
.font(Font.custom("Helvetica Neue", size: 24.0))
.padding(20)
.foregroundColor(Color.white)
.background(Color.purple)
.cornerRadius(12)
}
}.padding(20)
}
Run Code Online (Sandbox Code Playgroud)
我想要这两个特定元素的不同对齐方式。标题必须有一个前导对齐,但另一方面按钮位于中心。现在我将 VStack 对齐设置为领先。我不太熟悉 Swift UI,所以第一个想法是使用几个具有不同对齐方式的垂直堆栈,但我认为它可以更容易地完成。如果我向按钮添加对齐指南,它也不起作用。
我会使用包含HStack在下面的演示中
var body: some View {
VStack(alignment: .leading) {
Text("Title")
.bold()
.font(Font.custom("Helvetica Neue", size: 36.0))
HStack {
Spacer()
Button(action: {}){
Text("Create")
.bold()
.font(Font.custom("Helvetica Neue", size: 24.0))
.padding(20)
.foregroundColor(Color.white)
.background(Color.purple)
.cornerRadius(12)
}
Spacer()
}
}.padding(20)
}
Run Code Online (Sandbox Code Playgroud)
你可以用GeometryReader这个:
一个容器视图,将其内容定义为其自身大小和坐标空间的函数。
https://developer.apple.com/documentation/swiftui/geometryreader
GeometryReader { geometry in
VStack(alignment: .leading) {
Text("Title")
.bold()
.font(Font.custom("Helvetica Neue", size: 36.0))
Button(action: {
}) {
Text("Create")
.bold()
.font(Font.custom("Helvetica Neue", size: 24.0))
.padding(20)
.foregroundColor(Color.white)
.background(Color.purple)
.cornerRadius(12)
.frame(width: geometry.size.width / 2, height: geometry.size.height / 2, alignment: .center)
}
}.padding(20)
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
1357 次 |
| 最近记录: |