protocol ViewModifier
应用于视图或另一个视图修改器的修改器,产生原始值的不同版本。
struct BorderedCaption: ViewModifier{
func body(content: Content) -> some View {
content
.font(.caption2)
.padding(10)
.overlay {
RoundedRectangle(cornerRadius: 15)
.stroke(lineWidth: 1)
}
.foregroundColor(.blue)
}
}
extension View{
func borderedCaption() -> some View {
modifier(BorderedCaption())
}
}
struct ContentView1: View {
var body: some View{
Image(systemName: "bus")
.resizable()
.frame(width:50, height:50)
Text("Downtown Bus")
.borderedCaption()
}
}

SwiftUI-View-ViewModifier@2x.png