我希望藍色矩形中的文本 1 和文本 2 位于 的邊界內,Rectangle但是當使用疊加時,它似乎溢位給定的邊界之外。如何解決這個問題?
我也嘗試過ZStack,但結果相同。
import SwiftUI
struct ContentView: View {
var body: some View {
VStack {
ZStack {
Rectangle()
.stroke(lineWidth: 1)
.frame(width: 1, height: 55)
Rectangle()
.stroke(lineWidth: 10)
.cornerRadius(15)
.foregroundColor(Color.blue)
.frame(width: .infinity, height: 60)
.padding()
}
.overlay(
HStack {
Text("Text 1")
Spacer()
Text("Text 2")
}
)
}
}
}

uj5u.com熱心網友回復:
您需要將疊加層放置在不同的位置(修飾符的順序很重要!),例如
var body: some View {
VStack {
ZStack {
Rectangle()
.stroke(lineWidth: 1)
.frame(width: 1, height: 55)
Rectangle()
.stroke(lineWidth: 10)
.cornerRadius(15)
.foregroundColor(Color.blue)
.overlay(
HStack { // << here !!
Text("Text 1")
Spacer()
Text("Text 2")
}
.padding(.horizontal)
)
.frame(width: .infinity, height: 60)
.padding()
}
}
}

使用 Xcode 13 / iOS 15 測驗
轉載請註明出處,本文鏈接:https://www.uj5u.com/shujuku/386061.html
上一篇:一個尋路問題:如何判斷水路情況?
