用于自定义视图的 SwiftUI ViewModifier

Ang*_*han 7 swiftui viewmodifier swiftui-state

有没有办法创建修改器来更新@State private var正在修改的视图中的a ?

我有一个自定义视图,它返回Text带有“动态”背景颜色的 a 或Circle带有“动态”前景色的 a。

struct ChildView: View {
    var theText = ""
    
    @State private var color = Color(.purple)
    
    var body: some View {
        HStack {
            if theText.isEmpty {          // If there's no theText, a Circle is created
                Circle()
                    .foregroundColor(color)
                    .frame(width: 100, height: 100)
            } else {                      // If theText is provided, a Text is created
                Text(theText)
                    .padding()
                    .background(RoundedRectangle(cornerRadius: 25.0)
                                    .foregroundColor(color))
                    .foregroundColor(.white)
            }
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

我在我的应用程序的不同部分重复使用这个视图。如您所见,我需要指定的唯一参数是theText. 因此,创建此 ChildView 的可能方法如下:

struct SomeParentView: View {
    var body: some View {
        VStack(spacing: 20) {
            ChildView()   // <- Will create a circle

            ChildView(theText: "Hello world!")   // <- Will create a text with background
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

在此处输入图片说明

到目前为止没有什么花哨的。现在,我需要的是创建(也许)一个修饰符或类似的东西,以便在父视图中,如果我需要对该 ChildView 进行更多自定义,我可以将其值@State private var color.red其他颜色更改为其他颜色。我试图实现的示例:

struct SomeOtherParentView: View {
    var body: some View {
        HStack(spacing: 20) {
            ChildView()

            ChildView(theText: "Hello world!")
                .someModifierOrTheLike(color: Color.green)   // <- what I think I need
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

我知道我可以private从中删除关键字varcolor在构造函数中传递as 参数(例如:)ChildView(theText: "Hello World", color: .green),但我认为这不是解决此问题的方法,因为如果我需要对子视图进行更多自定义,我会最终得到一个非常大的构造函数。

那么,关于如何实现我正在寻找的任何想法?希望我自己解释:) 谢谢!!!

Asp*_*eri 9

这是您的视图,修饰符只是生成另一个修改后的视图的函数,所以......这里有一些可能的简单方法来实现您想要的。

使用 Xcode 12 / iOS 14 测试

演示

struct ChildView: View {
    var theText = ""
    
    @State private var color = Color(.purple)
    
    var body: some View {
        HStack {
            if theText.isEmpty {          // If there's no theText, a Circle is created
                Circle()
                    .foregroundColor(color)
                    .frame(width: 100, height: 100)
            } else {                      // If theText is provided, a Text is created
                Text(theText)
                    .padding()
                    .background(RoundedRectangle(cornerRadius: 25.0)
                                    .foregroundColor(color))
                    .foregroundColor(.white)
            }
        }
    }
    
    // simply modify self, as self is just a value
    public func someModifierOrTheLike(color: Color) -> some View {
        var view = self
        view._color = State(initialValue: color)
        return view.id(UUID())
    }
}
Run Code Online (Sandbox Code Playgroud)

  • 这当然超出了最初问题的范围......但请参阅涵盖新案例的更新变体。 (2认同)