你好!我試圖DatePicker
在Section
SwiftUI 中居中。螢屏截圖顯示當前顯示和所需顯示。
我嘗試了以下方法,但似乎不起作用:
- 將 DatePicker 放在 VStack 中,以 .alignment 為中心,
- 在部分上使用 .frame(alignment: .center),
- 并嘗試將中心裝飾器放置到 DatePicker 本身。
第一個 2^ 使該部分中的文本居中,但不是螢屏上實際的 DatePicker 物件本身。
關于如何做到這一點的解釋,以及對未來此類情況的了解將非常有幫助。謝謝!
NavigationView {
Form {
Section {
VStack(alignment: .center) {
DatePicker("Please enter a time", selection: $wakeUpTime, displayedComponents: .hourAndMinute)
.labelsHidden()
}
} header: {
Text("When do you want to wake up?")
.font(.headline)
}
.frame(alignment: .center)
.navigationTitle("BetterRest")
.toolbar {
Button("Calculate", action: calculateBedtime)
}
uj5u.com熱心網友回復:
您可以使用 a HStack
with twoSpacer
在 SwiftUI 中完成此輸出。
NavigationView {
Form {
Section {
HStack{ //Convert to HStack
Spacer() //add this
DatePicker("Please enter a time", selection: $wakeUpTime, displayedComponents: .hourAndMinute)
.labelsHidden()
Spacer() // add this
}
} header: {
Text("When do you want to wake up?")
.font(.headline)
}
.frame(alignment: .center)
.navigationTitle("BetterRest")
.toolbar {
Button("Calculate", action: calculateBedtime)
}
}
}
輸出:
uj5u.com熱心網友回復:
試試下面的代碼:
var body: some View {
NavigationView {
Form {
Section {
DatePicker("Please enter a time",
selection: $wakeUpTime,
displayedComponents: .hourAndMinute)
} header: {
Text("When do you want to wake up?")
.font(.headline)
}
.frame(alignment: .center)
.navigationTitle("BetterRest")
.toolbar {
Button("Calculate") {
print("Calculate")
}
}
}
}
}
轉載請註明出處,本文鏈接:https://www.uj5u.com/qita/505332.html