【问题标题】:How can I create an extension for a VStack with properties in SwiftUI?如何在 SwiftUI 中为具有属性的 VStack 创建扩展?
【发布时间】:2021-09-04 00:37:48
【问题描述】:

我想将属性列表应用于 VStack 而不是形状。以下代码通过在 Shape 扩展中定义的 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

【问题讨论】:

  • 如果你使用extension View而不是Shape会发生什么?它适用于两者。

标签: ios swift swiftui vstack


【解决方案1】:

解决方法是在VStack之后添加:

.background(
    RoundedRectangle(cornerRadius: 25) //the shape you want
    .innerN(Color.mph)
)
                                       

【讨论】:

    猜你喜欢
    • 2011-06-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-10-14
    • 1970-01-01
    • 2019-07-14
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多