无法输入文字栏位

时间:2019-06-12 03:06:29

标签: ios swift user-interface textfield swiftui

我正在使用SwiftUI框架创建SignUp屏幕。

我有两个文本字段FirstNameLastName和一个按钮。

我将它们分组在VStack中。

当我运行应用程序时,该按钮是可点击的,但是我无法在文本字段中键入任何内容。

这是我的代码:

@State var firstName = ""
@State var lastName = ""
var body: some View {

    NavigationView {

        VStack {

            VStack {

                VStack {

                    Group {
                       TextField($firstName, placeholder: Text("First Name")).padding(10)
                    }.background(Color.white).clipShape(RoundedRectangle(cornerRadius: 5))
                        .shadow(radius: 3)

                    Group {
                        TextField($lastName, placeholder: Text("Last Name")).padding(10)
                    }.background(Color.white).clipShape(RoundedRectangle(cornerRadius: 5))
                        .shadow(radius: 3)

                    Button(action: {
                    }) {
                        Group {
                            Text("Create User").color(.white).padding(10)
                        }.background(Color.blue).clipShape(RoundedRectangle(cornerRadius: 5))
                            .shadow(radius: 5)
                    }
                }.padding(12)
                .background(Color.gray)
                .shadow(radius: 5)

            }.background(Color.gray)

        }.navigationBarTitle(Text("Sign Up"))
    }
}

3 个答案:

答案 0 :(得分:1)

Forms.frmData.ctrSapling.Enabled = True
Forms.frmData.ctrSapling.Locked = False

答案 1 :(得分:0)

在这种特定情况下,似乎在颜色/不透明度效果之后设置shadow会引起问题。

要确保合成效果在阴影之前 呈现,您可以将.compositingGroup()添加到VStack-在shadow之前-这样就可以解决此问题

Documentation说:

  

一个合成组在此视图的祖先视图中进行合成效果,例如不透明度和混合模式,在此视图呈现之前生效。

修订代码

VStack {
    // [...]  
}
.padding(12)
.background(Color.gray)
.compositingGroup() // THIS
.shadow(radius: 5)
.navigationBarTitle(Text("Sign Up"))

可以通过这种方式进一步简化用户界面-实现相同的结果。

struct ContentView: View {

    @State var firstName = ""
    @State var lastName = ""

    var body: some View {

        NavigationView {

            VStack {

                    TextField($firstName, placeholder: Text("First Name"))
                        .padding(10)
                        .background(Color.white)
                        .cornerRadius(5)
                        .shadow(radius: 3)

                    TextField($lastName, placeholder: Text("Last Name"))
                        .padding(10)
                        .background(Color.white)
                        .cornerRadius(5)
                        .shadow(radius: 3)

                    Button(action: { }) {
                        Text("Create User")
                            .color(.white)
                            .padding(10)
                            .background(Color.blue)
                            .cornerRadius(5)
                            .shadow(radius: 5)
                    }
            }
            .padding(12)
            .background(Color.gray)
            .compositingGroup()
            .shadow(radius: 5)
            .navigationBarTitle(Text("Sign Up"))
        }

    }

}

答案 2 :(得分:-1)

您的代码不包含按钮操作

RelativeLayout