如何在 SwiftUI 中为具有属性的 VStack 创建扩展?
How can I create an extension for a VStack with properties in SwiftUI?
我想将属性列表应用于 VStack 而不是形状。以下代码通过在形状扩展中定义的 innerN 函数将我的属性列表应用于形状:
ZStack {
Color.mph
let shape = RoundedRectangle(cornerRadius: 25)
//let shape = Circle() //it works with any shapes
shape
.innerN(Color.mph)
.frame(width: 300, height: 300)
}
extension Shape {
public func innerN<S:ShapeStyle>
(_ fillContent: S) -> some View
{
ZStack {
self.fill(fillContent)
.overlay(
self
.stroke(Color.gray, lineWidth: 4)
.blur(radius: 4)
.offset(x: 2, y: 2)
.mask(self.fill(LinearGradient(Color.black, Color.clear)))
)
.overlay(
self
.stroke(Color.white, lineWidth: 8)
.blur(radius: 4)
.offset(x: -2, y: -2)
.mask(self.fill(LinearGradient(Color.clear, Color.black)))
)
}
}
}
我如何修改我的扩展程序以便我可以做类似的事情? :
VStack(spacing: 20){//...}
.padding()
.cornerRadius(25)
//.innerN(color) //with the shape of that VStack
解决方法是在VStack之后添加:
.background(
RoundedRectangle(cornerRadius: 25) //the shape you want
.innerN(Color.mph)
)
我想将属性列表应用于 VStack 而不是形状。以下代码通过在形状扩展中定义的 innerN 函数将我的属性列表应用于形状:
ZStack {
Color.mph
let shape = RoundedRectangle(cornerRadius: 25)
//let shape = Circle() //it works with any shapes
shape
.innerN(Color.mph)
.frame(width: 300, height: 300)
}
extension Shape {
public func innerN<S:ShapeStyle>
(_ fillContent: S) -> some View
{
ZStack {
self.fill(fillContent)
.overlay(
self
.stroke(Color.gray, lineWidth: 4)
.blur(radius: 4)
.offset(x: 2, y: 2)
.mask(self.fill(LinearGradient(Color.black, Color.clear)))
)
.overlay(
self
.stroke(Color.white, lineWidth: 8)
.blur(radius: 4)
.offset(x: -2, y: -2)
.mask(self.fill(LinearGradient(Color.clear, Color.black)))
)
}
}
}
我如何修改我的扩展程序以便我可以做类似的事情? :
VStack(spacing: 20){//...}
.padding()
.cornerRadius(25)
//.innerN(color) //with the shape of that VStack
解决方法是在VStack之后添加:
.background(
RoundedRectangle(cornerRadius: 25) //the shape you want
.innerN(Color.mph)
)