我正在尝试重新创建Twitter iOS应用的一部分以学习SwiftUI,并且想知道如何动态地将一个视图的宽度更改为另一视图的宽度。就我而言,使下划线与“文本”视图的宽度相同。
我已附上屏幕截图,以尝试更好地解释我的意思。任何帮助将不胜感激,谢谢!
这也是我到目前为止的代码:
import SwiftUI
struct GridViewHeader : View {
@State var leftPadding: Length = 0.0
@State var underLineWidth: Length = 100
var body: some View {
return VStack {
HStack {
Text("Tweets")
.tapAction {
self.leftPadding = 0
}
Spacer()
Text("Tweets & Replies")
.tapAction {
self.leftPadding = 100
}
Spacer()
Text("Media")
.tapAction {
self.leftPadding = 200
}
Spacer()
Text("Likes")
}
.frame(height: 50)
.padding(.horizontal, 10)
HStack {
Rectangle()
.frame(width: self.underLineWidth, height: 2, alignment: .bottom)
.padding(.leading, leftPadding)
.animation(.basic())
Spacer()
}
} …Run Code Online (Sandbox Code Playgroud)