堆栈内的SwiftUI文本字段锁定不可访问

堆栈内的SwiftUI文本字段锁定不可访问,swift,swiftui,textfield,Swift,Swiftui,Textfield,经过一段时间的故障排除(并将整个视图剥离到测试文件中),原因是堆栈中的Textfields“锁定”或不可访问,我发现这是堆栈中存在一些修改器的时候。 在这种情况下,这是因为一个阴影 可以理解,某些修改器应用于修改视图中的每个视图。但这应该很酷 因此我知道什么,但有人知道为什么吗 @State private var tempName: String = "" @State private var tempSurname: String = "" @Stat

经过一段时间的故障排除(并将整个视图剥离到测试文件中),原因是堆栈中的Textfields“锁定”或不可访问,我发现这是堆栈中存在一些修改器的时候。 在这种情况下,这是因为一个阴影

可以理解,某些修改器应用于修改视图中的每个视图。但这应该很酷

因此我知道什么,但有人知道为什么吗

@State private var tempName: String = ""
@State private var tempSurname: String = ""
@State private var tempPhone: String = ""

var body: some View {

        VStack  { //Stack 1
            Text("Please provide your details")
                VStack (spacing: 16) {
                       #warning ("check out why below textfields wont accept interaction")
                            TextField("User Name", text: $tempName)
                            TextField("User Surname", text: $tempSurname)
                            TextField("User Phone", text: $tempPhone)
                  }
                  .padding(.horizontal, 8)
                  .padding(.vertical, 16)
                  .shadow(radius: 8, x: 2, y: 4)
       }
}
可以理解,某些修改器应用于修改视图中的每个视图

没错

但是,您可以使用将修改器仅应用于最外层的视图:

VStack(spacing: 16) {
    // ...
}
.padding(.horizontal, 8)
.padding(.vertical, 16)
.compositingGroup() // add before `shadow`
.shadow(radius: 8, x: 2, y: 4)

以下是直接从文档中获得的一个非常好的解释:

/// Wraps this view in a compositing group.
///
/// A compositing group makes compositing effects in this view's ancestor
/// views, such as opacity and the blend mode, take effect before this view
/// is rendered.
///
/// Use `compositingGroup()` to apply effects to a parent view before
/// applying effects to this view.
///
/// In the example below the `compositingGroup()` modifier separates the
/// application of effects into stages. It applies the ``View/opacity(_:)``
/// effect to the VStack before the `blur(radius:)` effect is applied to the
/// views inside the enclosed ``ZStack``. This limits the scope of the
/// opacity change to the outermost view.
///
///     VStack {
///         ZStack {
///             Text("CompositingGroup")
///                 .foregroundColor(.black)
///                 .padding(20)
///                 .background(Color.red)
///             Text("CompositingGroup")
///                 .blur(radius: 2)
///         }
///         .font(.largeTitle)
///         .compositingGroup()
///         .opacity(0.9)
///     }
///
/// ![A view showing the effect of the compositingGroup modifier in applying
/// compositing effects to parent views before child views are
/// rendered.](SwiftUI-View-compositingGroup.png)
///
/// - Returns: A view that wraps this view in a compositing group.
@inlinable public func compositingGroup() -> some View

非常感谢。这正是问题所在,解释提供了我对此所需的洞察力。不要只是觉得这是正确的答案-从文件中可以看出,这是一个明确的答案