ViewModifier视图修饰符和网页设计里的CSS类似,主要用于整合一组常用的视图样式。当项目拥有很多具有相同样式的
示例代码:
struct ContentView : View { var body: some View { VStack(alignment: .center, spacing: 40){ Image("avarta1").modifier(myImageStyle()) Image("avarta2").modifier(myImageStyle()) Image("avarta3").modifier(myImageStyle()) } .padding() } } struct myImageStyle: ViewModifier { func body(content: Content) -> some View { content .frame(width: 200, height: 200, alignment: .leading) .cornerRadius(100) .clipped() .saturation(0.0) } }