我试图找出这两种方法之间的实际区别。例如:
struct PrimaryLabel: ViewModifier {
func body(content: Content) -> some View {
content
.padding()
.background(Color.black)
.foregroundColor(Color.white)
.font(.largeTitle)
.cornerRadius(10)
}
}
extension View {
func makePrimaryLabel() -> some View {
self
.padding()
.background(Color.black)
.foregroundColor(Color.white)
.font(.largeTitle)
.cornerRadius(10)
}
}
然后我们可以按照以下方式使用它们:
Text(tech.title)
.modifier(PrimaryLabel())
Text(tech.title)
.makePrimaryLabel()
ModifiedContent(
content: Text(tech.title),
modifier: PrimaryLabel()
)
struct ColorChangeView: View
? - Timur Bernikovich@State
变量在ViewModifier
中是允许的。最初的问题不是视图扩展和 ViewModifier 之间的区别吗?你现在有了一个答案;) - kontiki