如何在我的 SwiftUI 螢屏中擴展 Buttons 的寬度,以便它們占據螢屏的整個寬度減去 10 個點作為邊距?
這是我的代碼;
struct SelectView: View {
@State var resultsIsSelected = false
var body: some View {
ZStack {
Color(.systemTeal)
.edgesIgnoringSafeArea(.all)
VStack(spacing: 30) {
NavigationLink(isActive: $resultsIsSelected) {
ResultsView()
} label: {
Button {
resultsIsSelected = true
} label: {
Text("Search by first letter")
.foregroundColor(.white)
.font(.system(size: 25.0))
.padding()
.navigationTitle("Select Search Option")
.navigationBarTitleDisplayMode(.inline)
}
.background(.blue)
.clipShape(RoundedRectangle(cornerRadius: 30.0))
}
NavigationLink(isActive: $resultsIsSelected) {
ResultsView()
} label: {
Button {
resultsIsSelected = true
} label: {
Text("Search by cocktail name")
.foregroundColor(.white)
.font(.system(size: 25.0))
.padding()
}
.background(.blue)
.clipShape(RoundedRectangle(cornerRadius: 30.0))
}
NavigationLink(isActive: $resultsIsSelected) {
ResultsView()
} label: {
Button {
resultsIsSelected = true
} label: {
Text("Search by ingredient")
.foregroundColor(.white)
.font(.system(size: 25.0))
.padding()
}
.background(.blue)
.clipShape(RoundedRectangle(cornerRadius: 30.0))
}
}
}
}
}
struct SelectView_Previews: PreviewProvider {
static var previews: some View {
SelectView()
}
}
這是我的 SwiftUI 視圖的螢屏截圖

我已經在 Text 組件和 Button 組件以及 VStack 和 ZStack 上嘗試了 padding() 和 frame() 但沒有任何效果。我有一種感覺,這與 clipShape 修飾符或 RoundedRectangle() 有關
uj5u.com熱心網友回復:
你試過使用靈活的容器嗎?因此,根據按鈕框架/hstack,您可以使用 maxWidth: .infinity
uj5u.com熱心網友回復:
我添加.frame(UIScreen.main.bounds.width - 20.0)到 Text 組件。
轉載請註明出處,本文鏈接:https://www.uj5u.com/ruanti/329708.html
