struct ContentView: View {
var body: some View {
VStack (spacing: 20){
VStackCaseItem(alignment: .leading)//子視圖局左對齊
VStackCaseItem(alignment: .trailing)//子視圖局右對齊
VStackCaseItem(alignment: .center)//子視圖局中對齊
}
///使 VStack 填充屏幕的寬度
//方案一:設(shè)置 frame
// VStack (alignment: .leading) {
// Text("標(biāo)題")
// .font(.title)
// .background(Color.yellow)
// Text("內(nèi)容")
// .lineLimit(nil)
// .font(.body)
// .background(Color.blue)
// }
// .frame(
// maxWidth: .infinity,
// maxHeight: .infinity,
// alignment: .topLeading
// )
// .background(Color.red)
//方案二:結(jié)合 HStack 和 Spacer
// HStack {
// VStack(alignment: .leading) {
// Text("標(biāo)題")
// .font(.title)
// .background(Color.yellow)
// Text("內(nèi)容")
// .lineLimit(nil)
// .font(.body)
// .background(Color.blue)
// Spacer()
// }
// Spacer()
// }.background(Color.red)
}
}
struct ContentView_Previews: PreviewProvider {
static var previews: some View {
ContentView()
}
}
struct VStackCaseItem: View {
let alignment: HorizontalAlignment
var body: some View {
VStack(alignment: alignment, spacing: 20) {
Text("A\nB")
.frame(width: 50)
.background(Color.yellow)
Text(alignment.name)
.foregroundColor(.white)
.frame(width: 150)
.background(Color.red)
Text("oldBirds")
.background(Color.green)
.foregroundColor(.white)
}.background(Color.gray)
}
}
extension HorizontalAlignment {
var name: String {
switch self {
case .leading:
return "leading"
case .trailing:
return "trailing"
case .center:
return "center"
default:
return "other"
}
}
}
如果本文對你有幫助仅颇,歡迎點(diǎn)贊冯挎、評論颅眶、收藏…